Search code examples
javaregexreplaceall

Java - Regex Replace All will not replace matched text


Trying to remove a lot of unicodes from a string but having issues with regex in java.

Example text:

\u2605 StatTrak\u2122 Shadow Daggers

Example Desired Result:

StatTrak Shadow Daggers

The current regex code I have that will not work:

list.replaceAll("\\\\u[0-9]+","");

The code will execute but the text will not be replaced. From looking at other solutions people seem to use only two "\\" but anything less than 4 throws me the typical error:

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal Unicode escape sequence near index 2 \u[0-9]+

I've tried the current regex solution in online test environments like RegexPlanet and FreeFormatter and both give the correct result.

Any help would be appreciated.


Solution

  • I'm an idiot. I was calling the replaceAll on the string but not assigning it as I thought it altered the string anyway.

    What I had previously:

    list.replaceAll("\\\\u[0-9]+","");
    

    What I needed:

    list = list.replaceAll("\\\\u[0-9]+","");
    

    Result works fine now, thanks for the help.