0

I need to replace all '%' characters in string with "%25" but I don't want to replace % in %25 to avoid situation, when I get something like this %%25. I want to do it in Java. Example: input: % sdfsdaf %25 expected result: %25 sdfsdaf %25

Do you know what should I use to get it?

SantiBailors
  • 1,596
  • 3
  • 21
  • 44
up94up
  • 15
  • 5

1 Answers1

1

Without discussing much and digging deep into what you are really trying to accomplish with the replacement ... the answer to your question could be something like :

myString.replaceAll("%(?!25)", "%25");

From Pattern Documentation ; The (?!...) part means "only match if the text following (hence: lookahead) this doesn't (hence: negative) match this. But it doesn't actually consume the characters it matches (hence: zero-width).

Section 3.5 of the Link clarifies it in a bit more detail.

Exception_al
  • 1,049
  • 1
  • 11
  • 21