Java replaceAll() regex does not work

java, regex, special-characters

Solution

Strings are immutable. You forgot to reassign new `String` to the `s` variable :)

s = s.replaceAll("[^a-zA-Z0-9]+", "%");
 // ^ this creates a new String

Problem

I'm trying to replace all special characters with a "%", like: ``` "123.456/789" -> "123%465%798" ``` my regular expression is: ``` [^a-zA-Z0-9]+ ``` In online tools* it works perfecly, but in java ``` s.replaceAll("[^a-zA-Z0-9]+", "%"); ``` strings remain untouched. *I tried: http://www.regexplanet.com/ http://regex101.com/ and others

Original source

Related problems