Php-PCRE regular expression condition sub-group

Source: Internet
Author: User
PHP extension-text processing-PCRE regular expression syntax 13-condition sub-groups can enable a matcher based on the result of an asserted, or whether a previous capture sub-group matches a condition match a sub-group or select between two optional sub-groups. The condition sub-group syntax is as follows:

(?(condition)yes-pattern)(?(condition)yes-pattern|no-pattern)

If the conditions are met, use yes-pattern. Otherwise, use no-pattern (if specified ). If there are more than two optional sub-groups, a compilation error is generated.

There are two conditions. If the text is composed of numbers in the brackets of the condition, the condition is met when the child group represented by the number (before) is matched (yes-pattern is used ). Consider the following pattern. to make it easy to read, add some blank characters (view the PCRE_EXTENDED option) and divide it into three parts :(\()? [^ ()] + (? (1 )\))

The first part of the pattern matches an optional left brace. if this character appears, set it as the capture substring of the first child Group. The second part matches one or more non-parentheses. The third part is a condition sub-group, which will test whether the first sub-group matches. If yes, that is, the target string starts with braces and the condition is TRUE, if yes-pattern is used, an angle bracket must be matched. In other cases, since no-pattern does not appear, this sub-group does not match anything. In other words, this pattern matches a character sequence without parentheses or enclosed by parentheses.

If the conditional string (R) is used, it is used to obtain a recursive call to the mode or submode. The condition is always false at the "upper-level.

If the condition is not a numerical sequence or (R), it must be an asserted. The assertions here can be arbitrary, positive, negative, positive, and backward. Considering this mode, some white spaces are added to facilitate reading, and there are two optional paths in the second line.

(?(?=[^a-z]*[a-z])\d{2}-[a-z]{3}-\d{2}  |  \d{2}-\d{2}-\d{2} )

Conditional a positive asserted that matches an optional string of non-lowercase letters followed by a lowercase letter. In other words, it tests the target with at least one lower-case letter. if a lower-case letter is found, the target matches the first available Branch. In other cases, the target matches the second branch. This pattern matches strings in two formats: dd-aaa-dd or dd-dd. Aaa indicates lowercase letters, and dd indicates numbers.

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.