有人可以向我解释所有格量词吗? (常用表达)

发布于 2024-07-27 21:06:56 字数 504 浏览 2 评论 0原文

我正在阅读 PCRE 文档,它引用了所有格量词,但没有明确或具体定义它们。 我知道什么是贪婪量词,也知道什么是惰性量词。 但占有欲强?

PCRE 手册页 在使用该术语而不对其进行定义时似乎是作弊。 手册页特别指出术语“所有格量词”首先在 中定义弗里德尔的书。 嗯,那太好了,但是我没有弗里德尔的书,在阅读手册页时,我无法弄清楚所有格量词与贪婪量词的区别。

  • ? = 零或一,贪心
  • ? = 零或一,懒惰
  • ?+ = 零或一,所有格
  • '+' = 1 或多个,贪婪
  • +? = 一个或多个,惰性
  • ++ = 一个或多个,所有格

I am reading the PCRE doc, and it refers to possessive quantifiers, but does not explicitly or specifically define them. I know what a greedy quantifier is, and I know what a lazy quantifer is. But possessive?

The PCRE man page seems to be cheating when it uses the term without defining it. The man page specifically states that the term possessive quantifiers was first defined in Friedl's book. Well, that's great, but I don't have Friedl's book, and in reading the man page, between the lines, I cannot figure out what distinguishes possessive quantifiers from greedy ones.

  • ? = zero or one, greedy
  • ?? = zero or one, lazy
  • ?+ = zero or one, possessive
  • '+' = one or more, greedy
  • +? = one or more, lazy
  • ++ = one or more, possessive

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

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

发布评论

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

评论(1

情话已封尘 2024-08-03 21:06:56

也许最好的起点是正则表达式教程 - 所有格量词

讨论重复时
运算符或量词,我解释过
贪婪和懒惰的区别
重复。 贪婪和懒惰
确定正则表达式的顺序
引擎尝试可能的排列
正则表达式模式。 一个贪心的
量词将首先尝试重复
令牌尽可能多次,
并逐渐放弃比赛
引擎回溯以找到总体
匹配。 惰性量词首先会
重复令牌的次数为
并逐步扩大
当引擎回溯时匹配
正则表达式来查找整体匹配。


所有格量词是一种防止正则表达式引擎
尝试所有排列。 这主要对性能有用
原因。 您还可以使用所有格量词来消除某些
匹配。

Perhaps the best place to start is Regex Tutorial - Possessive Quantifiers:

When discussing the repetition
operators or quantifiers, I explained
the difference between greedy and lazy
repetition. Greediness and laziness
determine the order in which the regex
engine tries the possible permutations
of the regex pattern. A greedy
quantifier will first try to repeat
the token as many times as possible,
and gradually give up matches as the
engine backtracks to find an overall
match. A lazy quantifier will first
repeat the token as few times as
required, and gradually expand the
match as the engine backtracks through
the regex to find an overall match.


Possessive quantifiers are a way to prevent the regex engine from
trying all permutations. This is primarily useful for performance
reasons. You can also use possessive quantifiers to eliminate certain
matches.

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