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

问题描述 投票:3回答:1

如果我有一个正则表达式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.
.....

我的问题是,零长度匹配总是结束匹配程序吗?还有其他情况吗?

java regex
1个回答
1
投票

我的问题是,零长度匹配总是结束匹配程序吗?

不,你的输入字符串由一个单个字符a组成,因此它匹配一个零长度位置,更多字符导致更多匹配。

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

这取决于RegEx引擎。不同的风格以不同的方式处理零长度匹配。他们不会让同一位置的无限比赛发生:

Perl,PCRE将始终在上一场比赛结束时开始下一场比赛尝试,无论是否为零长度......

Python在零长度匹配后前进。搜索和替换的gsub()函数在前一个非零长度匹配结束的位置跳过零长度匹配。

More on zero-length matches

© www.soinside.com 2019 - 2024. All rights reserved.