I’m trying to figure out a regex pattern to use with the Java String.replaceAll() function. I need to replace all the %26 but I don’t want it to pick up any other numbers with a ’26’ prefix.
For example I want:
"abc%26def".replaceAll(regex, "&") to return "abc&def"
– and –
"abc%2623def".replaceAll(regex, "&") to return "abc%2623def" (no change)
I’m aware I can easily write a few more lines of code to accomplish this task but I was wondering if it’s possible to do this with just a single replaceAll.
You can use a negative lookahead assertion that prevents matches where
%26is followed by another digit (you’ll need to escape the\in Java, so it would be\\d):