Reputation: 195
I just want to use the regular expression to match the backslash (\
) character, but it throws a PatternSyntaxException
:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at helloworld.HelloWorld.main(HelloWorld.java:20)
Upvotes: 2
Views: 2352
Reputation: 30087
You're just trying a regex using only the regex escape character \
(that's why is raised java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 \
)
Just to be clear, incidentally, the slash \
in Java is also the character used to identify the start of a escaped sequence (java escape characters) and has special meaning to the compiler. So if you want write a slash in a String you have to double it ("\\"
).
If you want write a regex that search a slash you must escape it, and translating the regex in a Java String you must double the slashes again.
So the regex for slash becomes "\\\\"
Upvotes: 2