有人可以向我解释占有量词吗?(常用表达) [英] Can someone explain Possessive Quantifiers to me? (Regular Expressions)

查看:50
本文介绍了有人可以向我解释占有量词吗?(常用表达)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 PCRE 文档,它指的是 占有量词,但没有明确或具体地定义它们.我知道贪婪量词是什么,我知道什么是惰性量词.但占有欲?

PCRE 手册页 在使用该术语而未定义时似乎在作弊.手册页特别指出,术语 占有量词 最初在 弗里德尔的书.好吧,那太好了,但我没有 Friedl 的书,在阅读手册页时,在字里行间,我无法弄清楚所有格量词与贪婪量词的区别.

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

解决方案

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

<块引用>

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

<小时><块引用>

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

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

解决方案

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.

这篇关于有人可以向我解释占有量词吗?(常用表达)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆