I’m trying to create a valid Java regex for matching strings representing standard “military time”:
String militaryTimeRegex = "^([01]\d|2[0-3]):?([0-5]\d)$";
This gives me a compiler error:
Invalid escape sequence (valid ones are \b \t \n \f \r \” \’ \ )
Where am I going wrong?!?
Make sure you use double backslashes for escaping characters:
Single backslashes indicate the beginning of an escape sequence. You need to use
\\to get the character as it appears in theString.To answer your comment, you are currently only matching
19:00. You need to account for the additional:00at the end of theStringin your pattern: