Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Regex条件始终返回else_Java_Regex - Fatal编程技术网

Java Regex条件始终返回else

Java Regex条件始终返回else,java,regex,Java,Regex,我正试图为军事时间0000-2359编写一个正则表达式,但它可以在29小时内通过。为什么表达式没有抛出24XX+的错误 while(true) { try { sInput = input.nextLine(); // If the input is a properly formatted time break the loop // otherwise th

我正试图为军事时间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());
        }           
    }

你想看后面吗?你想看后面吗?我知道这已经得到了回答和接受,但我建议使用更简单、更明确的正则表达式,而不是使用lookbehinds:

[0-1][0-9]| 2[0-3][0-5][0-9]


它更短,在每个正则表达式引擎中都支持,对我来说至少更清晰。

我知道这已经得到了回答和接受,但我建议使用更简单、更明确的正则表达式,而不是使用lookbehinds:

[0-1][0-9]| 2[0-3][0-5][0-9]


它更短,每个正则表达式引擎都支持它,对我来说至少更清晰。

这样做仍然会产生同样的问题。今天早上无法键入。现在试试看,行得通!非常感谢。只有一个问题,为什么?我不知道。我想他们就是这样做的。这可能是一个很好的理由,或者根本没有理由。在某些版本中,例如Ruby,显然还有Java7?这样做仍然会产生同样的问题。今天早上无法键入。现在试试看,行得通!非常感谢。只有一个问题,为什么?我不知道。我想他们就是这样做的。这可能是一个很好的理由,或者根本没有理由。在一些口味中,比如Ruby,显然还有Java7?
[0-2]((?<=2)[0-3]|(?<!2)[0-9])[0-5][0-9]
                  \____/
                     |
add negative look-behind here