JDK-8144811 regex.Matcher.quoteReplacement doesn't quote '&' inside the replacement string

Thanh Hong Dai hdthanh at tma.com.vn
Fri Dec 18 11:03:20 UTC 2015


JDK-8144811: https://bugs.openjdk.java.net/browse/JDK-8144811

 

No repro on 8u66.

 

Test code:

 

import java.util.regex.Matcher;

 

 

public class JDK8144811 {

       public static void main(String args[]) {

              String s = "Dogs and cats are lovely pets but cost some money
to see a vet";

              

              s = s.replaceAll("money", Matcher.quoteReplacement("$"));

              

              String and_1 = s.replaceAll("and", "&");

              System.out.println(and_1);

              

              String and_2 = s.replaceAll("and",
Matcher.quoteReplacement("&"));

              System.out.println(and_2);

 

              String workaround = s.replaceAll("and",
Matcher.quoteReplacement("\\\\&"));

              System.out.println(workaround);

       }

}

Output:

 

Dogs & cats are lovely pets but cost some $ to see a vet

Dogs & cats are lovely pets but cost some $ to see a vet

Dogs \\& cats are lovely pets but cost some $ to see a vet

 

 

It's unclear what the bug reporter expects the result to be.




More information about the core-libs-dev mailing list