Whenever you write regular expressions in Java, remember to avoid the \ characters used in the line that defines the regular expression. In other words, if your regular expression contains one \ , you should write two \\ . For example, your code should look like this:
^(?:(?:([01]?\\d|2[0-3]):)?([0-5]?\\d):)?([0-5]?\\d)$
Why, you ask? because in Java strings \ is an escape character used to denote special characters (example: tabs, new lines, etc.), and if the string contains \ , then it itself must be escaped by adding another \ in front of it. Therefore, \\ .
For the record here , this is the Java language specification page that lists valid escape characters and their meanings, note the last:
\b backspace \t horizontal tab \n linefeed \f form feed \r carriage return \" double quote \' single quote \\ backslash
รscar Lรณpez
source share