Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping * character in java

I am trying to execute the following operation on a String.

    if (combatLog.contains("//*name//*")) {
        combatLog.replaceAll("//*name//*",glad.target.name);
    }

The slashes are my attempt to escape the *, as it doesn't work without them. I have also tried one slash, and slashes on contains or replaceAll individually. Thanks

like image 277
Peter F Avatar asked Nov 20 '25 18:11

Peter F


1 Answers

replaceAll() (counter-intuitively) takes a regex, not a string.
To escape a character for a regex, you need a double-backslash (doubled to escape the backslash from the string literal).

However, you don't want a regex. You should simply call replace() instead, which won't need any escaping.

like image 160
SLaks Avatar answered Nov 22 '25 06:11

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!