Php-pcre a detailed description of the post-reference of a regular expression

Source: Internet
Author: User
Outside a character class, a backslash immediately follows a number that is greater than 0 (and possibly one number) is a back reference to a capturing group that occurred before the pattern.

If the number that follows the backslash is less than 10, it is always a back reference, and if there are not so many capturing groups in the pattern, an error is raised. In other words, the quoted parentheses cannot be less than the number of references less than 10. Look at the "backslash" section above to see how the specific numbers are handled.

A back reference directly matches what is actually captured by the referenced capturing group in the target string, rather than matching the contents of the subgroup pattern. Therefore, the pattern (sens|respons) E and \1ibility will match "Sense and Sensibility" and "response and responsibility" without matching "sense and Responsib Ility ". If a case-sensitive match is forced on a back reference, such as ((? i) RAH) \s+\1 matches "rah rah" and "rah rah", but does not match "rah rah", even if the original capturing subgroup itself is case-insensitive. The thing to consider here is that the expected content of the back reference is exactly the same as that of the referenced capturing subgroup (we can, of course, make the case insensitive by setting internal options before the back reference, or by adding the pattern modifier, but This practice actually controls the behavior from the outside. )

There may be more than one back reference that references the same child group. A subgroup may not actually be used for a particular match, and any subsequent references to that subgroup will fail. For example, a pattern (a| ( BC) \2 always fails when matching a string beginning with "a" instead of "BC". Since there can be as many as 99 back references, all numbers immediately following the backslash may be a potential back reference count. If the pattern is followed by a numeric character after the back reference, some separators must be used for terminating the reference syntax. If the pcre_extended option is set, you can use a space to do so. In other cases, you can use an empty comment.

If a back reference appears inside the subgroup it refers to, its match fails. For example, (A\1) will not get any matches. However, this reference can be used for internal sub-pattern duplication. For example, the pattern (a|b\1) + matches any number of "a" strings and "ABA", "Ababba", and so on: because there is an optional path within the subgroup, there is a path in the optional path to complete the match, and after the match is complete, the back reference is able to reference the content. In each iteration of the sub-pattern, the back reference matches the string to which the subgroup matched the last iteration. In order to do this, the pattern must satisfy a condition that, at the first iteration, the pattern must be able to guarantee that no matching back reference is required. This condition can be implemented as the above example with an optional path, or it can be done by using a quantifier with a minimum value of 0 to be modified to a reference.

After PHP 5.2.2, the \g escape sequence can be used for absolute and relative references to sub-schemas. This escape sequence must be immediately followed by an unsigned number or a negative number, optionally wrapping the numbers with parentheses. Sequence \1, \g1,\g{1} is a synonym relationship. This usage eliminates the ambiguity that occurs when a backslash is used to describe a reverse reference immediately after the value. This escape sequence facilitates the distinction between a back reference and an octal numeric character, and makes it clearer that a back reference is followed by a text-matching number, such as \g{2}1.

The \g escape sequence immediately follows a negative number to represent a relative back reference. For example: (foo) (bar) \g{-1} can match the string "Foobarbar", (foo) (bar) \g{2} can match "Foobarfoo". This is used as an optional scheme in a long pattern to keep track of the subgroup ordinal of a reference to a specific subgroup of the previous group.

A back reference also supports a syntax description that uses a subgroup name, such as (? P=name) or PHP 5.2.2 can be useful \k<name> or \k ' name '. Support for \k{name} and \g{name} is also included in PHP 5.2.4.

The above is the Php-pcre regular expression after the detailed introduction of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.