I have a need to replace a particular token (in this case, ?) with another token (we can start with ! or something). String’s replaceAll method will work for this. But, I don’t want to replace the question mark if it happens to follow the token action. (That would be bad!)
I’ve tried text = text.replaceAll("[^a][^c][^t][^i][^o][^n]\\?","!"); but that didn’t work.
For example, I want "test.action?param=lol?omg"; to turn into test.action?param=lol!omg. I know I could do something silly like
text.replaceAll("action\\?","%%%CRAZYTOKEN%%%")
.replaceAll("\\?","!")
.replaceAll("%%%CRAZYTOKEN%%%","action?");
but that just seems like a waste of time, especially on large strings. I’d rather do it right.
You need a negative look behind
This asserts that the
?does not followaction