我正在尝试为军事时间(0000-2359)编写一个正则表达式,但它允许通过任何一个小时到 29。为什么表达式不抛出 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());
}
}