懒惰、贪婪还是什么? 寻找明确的正则表达式参考

发布于 2024-07-13 17:39:20 字数 363 浏览 7 评论 0原文

最近,在网络上的某个地方*,我发现了正则表达式的参考,它描述了贪婪的“第三种方式”,两种方式都不同 来自贪婪的 (.*) 和惰性 (.*?) 匹配。

我现在尝试搜索SO、谷歌搜索,甚至搜索我的浏览器历史记录,但无济于事。

谁能猜出我看到了什么?


澄清:它指的是对我来说是一个新的构造(类似于 .*+),而且我相信它甚至有一个名称(类似于“被动贪婪”,但可能不是) 。


* 我很欣赏“网络上的某个地方”与“在巴别塔图书馆”或“在曼德尔布罗特集中”一样有用,但请尝试提供帮助

Recently, somewhere on the web*, I found a reference for regular expressions which described a "third way" of greediness, different both
from greedy (.*) and lazy (.*?) matching.

I've now tried searching SO, Googling, and even searching my browser history, but to no avail.

Can anyone make a good guess at what it was I saw?


Clarification: it referred to what was for me a new construct (something like .*+), and I believe it even had a name for it (something like, but probably not, "passively greedy").


* I appreciate that "somewhere on the web" is about as helpful as "in the Library of Babel" or "in the Mandelbrot set", but please try to help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

溺ぐ爱和你が 2024-07-20 17:39:20

我认为你指的是“所有格”匹配。 Java 在此页面上对此进行了描述: http ://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

所有格量词,贪婪地
尽可能匹配和不匹配
退后,即使这样做会
让整体匹配成功。

语法与您所描述的相同 (.*+) 。

I think you are referring to "posessive" matching. Java describes it on this page: http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

Possessive quantifiers, which greedily
match as much as they can and do not
back off, even when doing so would
allow the overall match to succeed.

The syntax is the same as what you described (.*+) .

送你一个梦 2024-07-20 17:39:20

这也许? http://www.regular-expressions.info/repeat.html


懒惰的替代方案

在这种情况下,有一个比让 plus 变得懒惰更好的选择。 我们可以使用贪婪的加号和否定的字符类:<[^>]+>。

This maybe? http://www.regular-expressions.info/repeat.html


An Alternative to Laziness

In this case, there is a better option than making the plus lazy. We can use a greedy plus and a negated character class: <[^>]+>.

深海里的那抹蓝 2024-07-20 17:39:20

有各种不同的正则表达式包。 PCRE(与 Perl 兼容的正则表达式)(或多或少)在 Perl、Java、PHP 以及可能的其他语言中使用。 PCRE 手册页 可能被视为权威参考。 它描述了所有格量词(例如*+和++),它们是原子组的简写。

There are various different regex packages. PCRE (Perl-compatible regular expressions) are used (more or less) in Perl, Java, PHP and probably other languages. The PCRE man page might be regarded as the definitive reference. It describes possessive quantifiers (e.g. *+ and ++), which are a shorthand for atomic groups.

桃酥萝莉 2024-07-20 17:39:20

嗯,不完全是参考,但仍然不错。
掌握正则表达式

O'Reilly 也有一本“参考”书,但我看不到就此作证。 刚刚第一次看到。

Well, not exactly a reference, but good still.
Mastering Regular Expressions

There is also a "reference" book from O'Reilly, but I can't testify on it. Just saw it for the first time.

没有伤那来痛 2024-07-20 17:39:20

我总是保留一份正则表达式备忘单在我的立方体里很方便。

I always keep a copy of this regular expressions cheat sheet handy in my cube.

淡淡绿茶香 2024-07-20 17:39:20

谢谢你们。 恢复记忆的关键是“占有”,而不是“被动”。

这里有一些有用的参考:

Thank you all. The key to getting my memory back was "possessive", not "passive".

Here are a couple of useful references:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文