Perl (... (*接受) ...)+从不匹配
从 perlre 手册页:
如果“(*ACCEPT)”位于捕获组内,则这些组是 在遇到“(*ACCEPT)”时标记为结束。 例如:
'AB' =~ /(A (A|B(*ACCEPT)|C) D)(E)/x;
将匹配,$1 将是“AB”,$2 将是“B”...
但是,如果第二个捕获组有量词,则模式永远不会匹配:
'AB' =~ /(A (A|B(*ACCEPT)|C)+ D)(E)/x or die "No match"; #dies
^
这是为什么?将 + 替换为 * 或 {0,99} 没有什么区别。包含 (*ACCEPT)
的捕获组上的任何量词似乎都会阻止 *ACCEPT 工作。预先感谢您的任何帮助。
From the perlre man page:
If the "(*ACCEPT)" is inside of capturing groups then the groups are
marked as ended at the point at which the "(*ACCEPT)" was encountered.
For instance:'AB' =~ /(A (A|B(*ACCEPT)|C) D)(E)/x;
will match, and $1 will be "AB" and $2 will be "B" ...
However if the second capture group has a quantifier, the pattern never matches:
'AB' =~ /(A (A|B(*ACCEPT)|C)+ D)(E)/x or die "No match"; #dies
^
Why is this? Replacing the + with * or {0,99} makes no difference. Any quantifier on a capture group which encloses the (*ACCEPT)
seems to prevent the *ACCEPT from working. Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 Perl 5.36 中修复的错误,正则表达式不匹配。 PCRE 和 PCRE2 库不受此错误影响;使用量词,正则表达式将“ABCDE”中的“AB”以及 regex101 网站上的整个字符串“ABDE”与 PCRE 和 PCRE2 相匹配。
The regex doesn't match because of a bug fixed in Perl 5.36. The PCRE and PCRE2 libraries aren't affected by this bug; with the quantifier, the regex matches the "AB" in "ABCDE" and the entire string "ABDE" on regex101 website with both PCRE and PCRE2.