I'm trying to write a regex for military time (0000-2359), but it lets through any hour up to 29. Why doesn't the expression throw the error for 24XX+?
while(true)
{
try
{
sInput = input.nextLine();
// If the input is a properly formatted time break the loop
// otherwise throw invalidTimeFormatException
if(Pattern.matches("[0-2](?:(?=2)[0-3]|[0-9])[0-5][0-9]", sInput))
{
// This will only happen if the time is properly formatted
// thanks to the regular expression above.
break;
}
throw invalidTimeFormatException;
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}