这应该返回 true,但返回 false。
"ADC".matches("A.*?C")
我已经在 javascript 正则表达式测试器上对其进行了测试:http://regexpal.com/
而且它有效,为什么它在 Java 中不起作用?
编辑: 与此相同:
System.out.println("DEAGHHF".matches("(A.*?C|C.*?A|D.*?C|C.*?D|A.*?F|F.*?A)"));
返回 false,regexpal 返回 true(与其他 javascript regex 引擎一样)。
最佳答案
不,it returns true .
System.out.println("ADC".matches("A.*?C"));
打印true
。
regexpal.com 的实现似乎有问题(这是可以理解的,因为它是 0.1.4 版)。尝试反复输入 ABC
。只有每一秒 ABC
都会被拒绝。 (至少在我的 firefox 版本中查看时是这样。)
关于您的修改:
A.?C|C.?A|D.?C|C.?D|A.?F|F.?A
被解释为
A.*?C or
C.*?A or
D.*?C or
C.*?D or
A.*?F or
F.*?A
换句话说
Something that starts with A and ends with C, or
Something that starts with C and ends with A, or
Something that starts with D and ends with C, or
....
Something that starts with F and ends with A,
由于 "DEAGHHF
"以 D
开头并以 F
结尾,因此它不会匹配。
也许您正在寻找 Matcher.find
method
关于Java Regex 找不到匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5923603/