gpt4 book ai didi

Php、正则表达式、反向引用

转载 作者:搜寻专家 更新时间:2023-10-31 20:44:10 24 4
gpt4 key购买 nike

我试图理解以下段落(来自 Php.net:Link):

However, if the decimal number following the backslash is less than 10, it is always taken as a back reference, and causes an error only if there are not that many capturing left parentheses in the entire pattern. In other words, the parentheses that are referenced need not be to the left of the reference for numbers less than 10. A "forward back reference" can make sense when a repetition is involved and the subpattern to the right has participated in an earlier iteration.

据我所知,如果小数点小于 10,则计算模式的所有左括号,如果数字大于 10,则仅计算我使用引用时的左括号(例如\13 ) 被计算在内。

例如:

假设我们有这个简单的模式:

'/^(a)(b)(c)(d)\6(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)$/';

This is the string we testing: abcdfefghikjklmn

php.net 说:

In other words, the parentheses that are referenced need not be to the left of the reference for numbers less than 10

因此,如示例所示,引用的括号在右侧且引用编号小于 10,那么为什么 preg_match 返回 0?

如果有人可以帮助理解这一段,谢谢大家,祝你有美好的一天。

最佳答案

Outside a character class, a backslash followed by a digit greater than 0 (and possibly further digits) is a back reference to a capturing subpattern earlier (i.e. to its left) in the pattern, provided there have been that many previous capturing left parentheses.

However, if the decimal number following the backslash is less than 10, it is always taken as a back reference, and causes an error only if there are not that many capturing left parentheses in the entire pattern. In other words, the parentheses that are referenced need not be to the left of the reference for numbers less than 10. A "forward back reference" can make sense when a repetition is involved and the subpattern to the right has participated in an earlier iteration.

\n解释(n > 0 总是):

  • n <= 9 : 始终向后引用 n第组(无论捕获组在哪里)。如果少于 n,则会导致错误捕获组。
  • n >= 10 : 如果至少有 n 则只有反向引用在它之前捕获组(开始)的数量。否则它是一个转义序列,就像在常规字符串中一样。

关于Php、正则表达式、反向引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15297386/

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