懒惰、贪婪还是什么? 寻找明确的正则表达式参考
最近,在网络上的某个地方*
,我发现了正则表达式的参考,它描述了贪婪的“第三种方式”,两种方式都不同 来自贪婪的 (.*)
和惰性 (.*?)
匹配。
我现在尝试搜索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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为你指的是“所有格”匹配。 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
The syntax is the same as what you described (.*+) .
这也许? 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: <[^>]+>.
有各种不同的正则表达式包。 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.
嗯,不完全是参考,但仍然不错。
掌握正则表达式
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.
我总是保留一份正则表达式备忘单在我的立方体里很方便。
I always keep a copy of this regular expressions cheat sheet handy in my cube.
谢谢你们。 恢复记忆的关键是“占有”,而不是“被动”。
这里有一些有用的参考:
Thank you all. The key to getting my memory back was "possessive", not "passive".
Here are a couple of useful references: