gpt4 book ai didi

php - 所有格量词和一次性子模式之间的区别

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:05:41 24 4
gpt4 key购买 nike

我正在阅读 PCRE 文档,我注意到所有格量词 + 和一次性子模式 (?>)(也称为原子组)在某种程度上类似于概念。有什么本质区别吗?

最佳答案

(?>)其实就是原子分组。

来自 Atomic Grouping on regular-expressions.info :

An atomic group is a group that, when the regex engine exits from it, automatically throws away all backtracking positions remembered by any tokens inside the group. Atomic groups are non-capturing. The syntax is (?>group).

来自 Possessive Quantifiers on regular-expressions.info :

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.

来自同一页面:

Technically, possessive quantifiers are a notational convenience to place an atomic group around a single quantifier. All regex flavors that support possessive quantifiers also support atomic grouping. But not all regex flavors that support atomic grouping support possessive quantifiers. With those flavors, you can achieve the exact same results using an atomic group.

Basically, instead of X*+, write (?>X*). It is important to notice that both the quantified token X and the quantifier are inside the atomic group. Even if X is a group, you still need to put an extra atomic group around it to achieve the same effect. (?:a|b)*+ is equivalent to (?>(?:a|b)*) but not to (?>a|b)*. The latter is a valid regular expression, but it won't have the same effect when used as part of a larger regular expression.

关于php - 所有格量词和一次性子模式之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18292247/

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