Java 零长度匹配是否结束匹配过程?

Java 零长度匹配是否结束匹配过程?,java,regex,Java,Regex,如果我有一个正则表达式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 "

如果我有一个正则表达式
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
组成,因此它与后面的一个零长度位置匹配,更多的字符导致更多的匹配

匹配过程在零长度匹配后结束。否则我会的 得到无限

这取决于RegEx引擎。不同的口味以不同的方式处理零长度匹配。但它们不允许在同一位置发生无限次匹配:

Perl、PCRE总是在测试结束时开始下一次匹配尝试 上一个匹配,不管长度是否为零

Python在零长度匹配后前进。函数的作用是 “搜索和替换”将在匹配的位置跳过零长度匹配 上次非零长度匹配已结束


显然,只要模式在可能匹配的最右侧位置接受长度为零的字符串,它就会这样做。去掉正则表达式中的问号,你只能得到一个匹配“a”。你能给我一个Java的exmaple吗?匹配过程在零长度匹配后继续?我对此感到困惑。