I am trying to use a pattern check against a string, and for some reason it is saying that strings that shouldn't match, do..
Code:
private static final Pattern VALID_TOKEN = Pattern.compile("^[a-zA-Z0-9\\{\\}\\[\\].+-/=><\\\\*]*$");
System.out.println(VALID_TOKEN.matcher(token).matches());
Examples:
"123" = true
"1,3" = true // Should NOT BE TRUE
"123*123" = true
"123*^123" = false
All of the above examples are correct except "1,3" the pattern should not include a COMMA. Does anyone have any ideas?
You need to escape the dash in
+-/
Otherwise, it is interpreted as a range from '+' to '/' - a range that includes '+', ',', '-'. '.', and '/'.
private static final Pattern VALID_TOKEN = Pattern.compile("^[a-zA-Z0-9\\{\\}\\[\\].+\\-/=><\\\\*]*$");
// Here ------------------------------------------------^^
Alternatively, you can move the dash to the end of the character class (i.e. put it before the closing ]).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With