【发布时间】:2018-03-01 14:49:47
【问题描述】:
如果我有一个正则表达式a? 和一个字符串a,我会得到:
Enter your regex: a?
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.
匹配过程在零长度匹配后结束。否则我会得到无限的:
I found the text "" starting at index 1 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.
.....
我的问题是,零长度匹配是否总是结束匹配过程?还有其他情况吗?
【问题讨论】:
-
显然,只要该模式在可能匹配的最右边位置接受零长度字符串,它就会这样做。从你的正则表达式中删除问号,你只会得到一个匹配“a”。