Using the Java ReplaceAll method in the project today replaces the backslash ("\") in the string with an empty string (""), resulting in the following exception:
1 java.util.regex.PatternSyntaxException:Unexpected Internal Error near index 1 \^
Surf the internet for the wrong reason: in the regex "\ \" means a "\", in Java, a "\" also use "\ \" to indicate. In this way, the previous "\ \" represents "\" in the Regex, and the latter "\ \" represents "\" in Java. so to use the ReplaceAll method to replace the backslash ("\") in the string with an empty string (""), you need to write this: Str.replaceall ("\\\\", "");
Write a test code to demonstrate the exception that appears above:
1 String s= "C: \ Disk"; 2 s.replaceall ("\ \", "");
Using the code above will cause
1 Unexpected internal error near index 1 \^
To replace the "\" in the "C: \ Disk" With an empty string, the correct notation is
1 s.replaceall ("\\\\", "" ");
This will allow you to replace the normal.