Sasi Kumar
Sasi Kumar

Reputation: 3

java.util.regex.PatternSyntaxException when trying to match with regex

When I am trying to execute the below code

text.matches("[a-zA-Z0-9 !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~]");

I am getting exception

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 43
[a-zA-Z0-9 !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]
                                           ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.clazz(Unknown Source)
    at java.util.regex.Pattern.sequence(Unknown Source)
    at java.util.regex.Pattern.expr(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 java.util.regex.Pattern.matches(Unknown Source)
    at java.lang.String.matches(Unknown Source)
    at test.G3Utils.checkIsAttribANS(G3Utils.java:47)
    at test.G3Utils.main(G3Utils.java:6)

Please help me to solve this

Upvotes: 0

Views: 2627

Answers (3)

Luizgrs
Luizgrs

Reputation: 4873

When using literal ] within a list in regex you should put it as the first characters otherwise the Parser will not understand it. However, Java also accepts escaping it, see next paragraph.

And for Java you need to escape [, with \, but you need to escape it in Java to use it as literal string, so replace [ by \\[

This will make your Regex work:

text.matches("[]a-zA-Z0-9 !\"#$%&'()*+,-./:;<=>?@\\[^_`{|}~]");

Other thing note that ,-. is matching the interval from comma until dot, if that's not the desired behavior move the - to the last position. (It works because the ASCII table order is ,, - and .).

Upvotes: 1

Stephan
Stephan

Reputation: 43033

You can improve your regex like this:

"[\\w !\"#$%&'()*+,-./:;<=>?@\\[\\]^_`{|}~]"

Here you can take advantage of the predefined character class \w.
\w is equivalent to [a-zA-Z0-9].

Reference: Java Regular Expressions: Predefined Character Classes

Upvotes: 0

Matthew Franglen
Matthew Franglen

Reputation: 4532

You need to escape the [ character like you have escaped the ] character.

So the fixed version of your regex is:

[a-zA-Z0-9 !\"#$%&'()*+,-./:;<=>?@\\[\\]^_`{|}~]

Upvotes: 2

Related Questions