Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String regex replace methods remove backslashes from replacement

Tags:

java

string

regex

I just lost 1 hour to track what I consider not normal behaviour with replaceAll/replaceFirst in the String class.

If there is a backslash in the replacement String then they are removed when replacing. I then read that you can use Matcher.quoteReplacement(String) to create a proper replacement string,but my question is why? I can expect that the first argument should be escaped with Patter.quote(String) if you don't want the special meaning but I don't see a reason to change the replacement :(

Yes I will start using replace(CharSequence,CharSequence), just want to know why :)

Here is an example that clearly shows the "strange" behaviour:

public static void main(String[] args) {
    String out = "\\\\test\\\\";
    System.out.println(out);
    String result = "a".replaceAll("a", out);
    System.out.println(result);
}

note how second line is with only single backslashes as opposed to two like in the first line

like image 814
nikolavp Avatar asked May 26 '26 19:05

nikolavp


1 Answers

Yes it is true that backslash needs to be doubly escaped as the first argument in String#replaceAll.

Reason:

It is due to the fact that your replacement String can contain back-references like $1, $2 etc even replacement text is also processed by underlying regex engine hence the need for double escaping same as the first argument, as you have also found out.

like image 152
anubhava Avatar answered May 28 '26 08:05

anubhava



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!