【问题标题】:Does zero-length matches end the match procedure?零长度匹配是否结束匹配过程?
【发布时间】: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”。

标签: java regex


【解决方案1】:

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

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

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

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

Perl , PCRE 总是在结束时开始下一次匹配尝试 上一场比赛,不管它是否是零长度的......

Python 在零长度匹配后前进。 gsub() 函数 search-and-replace 在 上一个非零长度匹配结束。

More on zero-length matches

【讨论】:

  • 你能给我一个Java的例子吗?匹配过程在零长度匹配后继续吗?我对此感到困惑。
猜你喜欢
  • 1970-01-01
  • 2016-04-02
  • 2013-08-19
  • 2017-05-23
  • 2018-04-28
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多