gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:48:01 25 4
gpt4 key购买 nike

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

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

最佳答案

My question is, does Zero-Length Matches always end the match procedure?

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

The match procedure ends after a zero-length match. Otherwise I will get infinite.

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

Perl , PCRE is to always start the next match attempt at the end of the previous match, regardless of whether it was zero-length or not...

Python advances after zero-length matches. The gsub() function to search-and-replace skips zero-length matches at the position where the previous non-zero-length match ended.

More on zero-length matches

关于java - 零长度匹配是否结束匹配过程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49052271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com