--tox=9 看起来第二个\S+ 是非贪婪的!仅-6ren">
gpt4 book ai didi

regex - 正则表达式中的贪婪运算符在 Tcl 8.5 中不起作用

转载 作者:行者123 更新时间:2023-12-01 09:36:51 26 4
gpt4 key购买 nike

查看这个简单的正则表达式代码:

puts [ regexp -inline {^\-\-\S+?=\S+} "--tox=9.0" ]

输出是:
 >--tox=9

看起来第二个\S+ 是非贪婪的!仅匹配 1 个字符
在 PERL 中,可以看到结果如我所料,见 1 行输出:
perl -e '"--tox=9.0" =~/(^\-\-\S+?=\S+)/ ; print "${1}\n"'
--tox=9.0

如何在 Tcl 中获得 Perl 行为?

最佳答案

这是 Tcl 的正则表达式实现的固有“特性”。例如,below来自亨利·斯宾塞(我相信,即使不是所有 Tcl 的正则表达式工作,他也做得最多)

It is very difficult to come up with an entirely satisfactory definition of the behavior of mixed-greediness regular expressions. Perl doesn't try: the Perl "specification" is a description of the implementation, an inherently low-performance approach involving trying one match at a time. This is unsatisfactory for a number of reasons, not least being that it takes several pages of text merely to describe it. (That implementation and its description are distant, mutated descendants of one of my earlier regexp packages, so I share some of the blame for this.)

When all quantifiers are greedy, the Tcl 8.2 regexp matches the longest possible match (as specified in the POSIX standard's regular-expression definition). When all are non-greedy, it matches the shortest possible match. Neither of these desirable statements is true of Perl.

The trouble is that it is very, very hard to write a generalization of those statements which covers mixed-greediness regular expressions -- a proper, implementation-independent definition of what mixed-greediness regular expressions should match -- and makes them do "what people expect". I've tried. I'm still trying. No luck so far.

The rules in the Tcl 8.2 regexp, which basically give the whole regexp a long/short preference based on its subexpressions, are the best I've come up with so far. The code implements them accurately. I agree that they fall short of what's really wanted. It's trickier than it looks.



基本上,具有混合贪婪和非贪婪量词的表达式会影响实现的简单性和性能。因此,实现使得量词的第一个“类型”被传递给所有其他量词。

换句话说,如果第一个量词是贪婪的,那么所有其他量词都是贪婪的。如果第一个是非贪婪的,那么所有其他人都将是非贪婪的。因此,您不能强制 Tcl 正则表达式像 Perl 正则表达式一样工作(或者您可以通过 exec 并使用 perl 的 bash 命令版本,但我对此并不熟悉)。

我建议使用否定类和/或 anchor 而不是非贪婪。

由于我不知道您问题的确切上下文,因此我不会提供替代正则表达式,因为这将取决于这是否真的是您尝试匹配的整个字符串。

关于regex - 正则表达式中的贪婪运算符在 Tcl 8.5 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29228393/

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