Php-pcre Regular Expression Conditional subgroup

Source: Internet
Author: User
Tags alphabetic character
You can make the match based on the result of an assertion, or if one of the previous capturing subgroups matches to match a subgroup of the criteria or choose between two optional subgroups. The two syntaxes of the conditional subgroup are as follows:

(? (condition) Yes-pattern) (? ( Condition) Yes-pattern|no-pattern)

If the condition is met, use Yes-pattern, and other cases use No-pattern (if specified). If there are more than 2 optional subgroups, a compile-time error is generated.

A total of two conditions. If the text consists of numbers in parentheses (condition), the condition is met (that is, using Yes-pattern) when the number represents (the previous) subgroup is matched. Consider the following pattern, in order to make it easier to read it adds some whitespace characters (see pcre_extended option) and divides it into three parts: (\ ()? [^()]+ (? (1) \))

The first part of the pattern matches an optional opening parenthesis, and if the character appears, set it as the capture substring of the first subgroup. The second part matches one or more non-parenthesis characters. The third part is a conditional subgroup, which tests if the first subgroup matches, and if it matches, that is, the target string starts with an opening parenthesis and the condition is true, then using Yes-pattern is where you need to match a closing parenthesis. In other cases, since No-pattern does not appear, this subgroup does not match anything. In other words, this pattern matches a sequence of characters that is not enclosed in parentheses or enclosed in parentheses.

If the conditional string (R), it is satisfied when the recursive invocation of the pattern or sub-pattern is obtained. In "Most superior", the condition is always false.

If the condition is not a sequence of numbers or (R), it must be an assertion. The assertions here can be arbitrary, positive, negative, positive, and forward. Consider this pattern, adding some whitespace characters for readability, and two optional paths on the second line.

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

Conditional positive assertion that matches an optional non-lowercase alphabetic character sequence followed by a lowercase letter. In other words, it tests for at least one lowercase letter in the target, and if lowercase letters are found, the target matches the first optional branch, and in other cases it matches the second branch. This pattern matches strings in two formats: DD-AAA-DD or DD-DD-DD. AAA represents lowercase letters, and DD is a number.

  • 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.