Java regex not compiling

I have the code

private String regexHHMM = "^([0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$";

The code does not compile with the error "Unclosed Character class"

This must be something very basic, is there any particular escaping I should be using, also I am mainly interested on WHY it cannot be accepted by the java compiler.

UPDATE: I have tried

Pattern.quote("^([0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$"); 

And now it compiles, but the regex does not match the HH:MM format now...

Upvotes: 0

Views: 198

Answers (1)

hwnd
hwnd

Reputation: 70732

You need to remove the initial open square bracket inside of your pattern.

^([0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$
  ^

Should be:

^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$

Upvotes: 4

Related Questions