Java COMMA Template Controller

I'm trying to use pattern validation on a string, and for some reason it says that strings that shouldn't be matched 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 for "1.3", the template must not include COMMA. Does anyone have any idea?

+3


source to share


1 answer


You need to get out of the dash into

+-/

      

Otherwise, it is interpreted as the range of '+'

up to '/'

- a range which 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 ]

).

+2


source







All Articles