Hide Forgot
Description of problem: $ cat StrTest.java public class StrTest { public static void main (String[] args) { String test = "abc\"def"; System.out.println("Test string is :" + test + ":"); test = test.replaceAll("\\", ""); System.out.println("Test string now :" + test + ":"); } } $ javac StrTest.java $ java StrTest Test string is :abc"def: Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 \ ^ at java.util.regex.Pattern.error(Pattern.java:1955) at java.util.regex.Pattern.compile(Pattern.java:1702) at java.util.regex.Pattern.<init>(Pattern.java:1351) at java.util.regex.Pattern.compile(Pattern.java:1028) at java.lang.String.replaceAll(String.java:2223) at StrTest.main(StrTest.java:5) zsh: exit 1 java StrTest $ rpm -qf $(readlink -f $(which java)) java-1.8.0-openjdk-headless-1.8.0.71-2.b15.el7_2.x86_64
Yes, the pattern is wrong. this is only java slash escape. test = test.replaceAll("\\", ""); Regeex is second round of escaping. So to escape SINGLE slash, you need test = test.replaceAll("\\\\", ""); However, slash is NOT in your original string of String test = "abc\"def"; So I'm not sure what was yours goal. Maybe; public static void main (String[] args) { String test = "abc\\\"def"; System.out.println("Test string is :" + test + ":"); test = test.replaceAll("\\\\", ""); System.out.println("Test string now :" + test + ":"); } : Test string is :abc\"def: : Test string now :abc"def: So... this is not a bug. This is very correct behaviour.
The regex is wrong, yes, but the issue was "Unexpected internal error," which sounds like a JVM internal issue not a regex pattern issue. Thanks.
Still it is PatternSyntaxException. And although I agree that " Unexpected internal error near index 1\^" is not exactly est message. It is correclty pointing to position in your regex(^\) and also in java code where it happened (at java.util.regex.Pattern.error(Pattern.java:1955)), So I would not claim it as bug. If you disagree, feel free to reopen.
I saw PatternSyntaxException earlier so when I see this "Unexpected internal error" I thought this is something different. I guess it would be better to have consistent error messages in every case possible but I'll leave it up to you to make the final call whether this is worth changing or not. Thanks.