我正在尝试将字符串iso_schematron_skeleton_for_xslt1.xsl
与regexp ([a-zA-Z|_])?(\w+|_|\.|-)+(@\d{4}-\d{2}-\d{2})?\.yang
进行匹配。
预期的结果是false
,它应该不匹配。
问题在于对matcher.matches()
的调用永远不会返回。
这是Java regexp实现中的错误吗?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloWorld{
private static final Pattern YANG_MODULE_RE = Pattern
.compile("([a-zA-Z|_])?(\\w+|_|\\.|-)+(@\\d{4}-\\d{2}-\\d{2})?\\.yang");
public static void main(String []args){
final Matcher matcher = YANG_MODULE_RE.matcher("iso_schematron_skeleton_for_xslt1.xsl");
System.out.println(Boolean.toString( matcher.matches()));
}
}
我正在使用:
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b15)
OpenJDK 64-Bit Server VM (build 25.181-b15, mixed mode)
最佳答案
该模式包含嵌套的量词。 \w+
在本身由+
量化的组内,这使得正则表达式引擎难以处理不匹配的字符串。使字符类脱离交替组更有意义,即(\\w+|_|\\.|-)+
=> [\\w.-]+
。
请注意,\w
已经与_
匹配。另外,字符类中的|
与文字|
char相匹配,而[a|b]
与a
,|
或b
相匹配,因此似乎您应该从第一个字符类中删除|
。
用
.compile("[a-zA-Z_]?[\\w.-]+(?:@\\d{4}-\\d{2}-\\d{2})?\\.yang")
请注意,您可以使用非捕获组(
(?:...)
)而不是捕获组,以避免不必要的开销,因为您只是在检查匹配项而不提取子字符串。请参阅regex demo(由于该模式与
matches()
一起使用,因此需要完整的字符串匹配,因此我在regex演示中添加了^
和$
)。