PHP-PCRE regular expression child Group (child mode)

Source: Internet
Author: User
PHP extension-text processing-PCRE regular expression syntax 8-child groups (Child Mode) child groups are defined by parentheses and can be nested. Marking part of a mode as a sub-group (sub-mode) mainly involves two tasks:

Localize available branches. For example, the pattern cat (arcat | erpillar |) matches "cat", "cataract", and "caterpillar". if there are no parentheses, it matches "cataract ", "erpillar" and an empty string.

Set the sub-group to capture the sub-group (defined above ). after the entire pattern match, the part of the target string that matches the sub-group will be passed back to the caller through the ovector parameter of pcre_exec. The order in which left parentheses appear from left to right is the subscript of the corresponding sub-group (starting from 1). you can use these subscript numbers to obtain the capture sub-pattern matching result.

For example, if the string "the red king" is matched using the (red | white) (king | queen) mode, the result of the pattern match is array ("red king ", "red king", "red", "king"), where 0th elements are the results of the entire pattern match, the following three elements are matched by the three sub-groups in sequence. The following tables are respectively 1, 2, and 3.

In fact, the two functions of parentheses are not always useful. We often need to use sub-groups for grouping, but do not need to capture them (separately. The left parentheses defined by the child group follow the string "? : "Will prevent this subgroup from being captured separately and will not affect the calculation of the subsequent subgroup sequence number. For example, if the string "the white queen" matches the pattern ((? : Red | white) (king | queen), the matched results will be array ("white queen", "white queen", "white queen "), and king | queen. The maximum number of sub-groups to be captured is 99, and the maximum number of all sub-groups (including captured and non-captured) allowed to be owned is 200.

To facilitate shorthand, if you need to set options at the beginning of a non-capturing sub-group, the option letter can be located? And:, for example:

(?i:saturday|sunday)(?:(?i)saturday|sunday)

The above two methods are actually the same. Because the optional branch tries each branch from left to right, and the option is not reset before the child mode ends, and because the option setting affects other branches, the above pattern matches "SUNDAY" and "Saturday ".

In PHP 4.3.3, you can use (? P Pattern. This sub-mode will appear in the matching results in both its name and order (numerical subscript). in PHP 5.2.2, two kinds of sub-group naming syntax are added :(? Pattern) and (? 'Name' pattern ).

You can select a sub-group in a regular expression if multiple matches are required. In order to allow multiple sub-groups to share one backward reference number ,(? | The syntax allows you to copy numbers. Consider the following regular expression matching Sunday:

(?:(Sat)ur|(Sun))day

Here, when backward reference 1 is null, Sun is stored in backward reference 2. when backward reference 2 does not exist, Sat is stored in backward reference 1. Use (? | Modify the mode to fix this problem:

(?|(Sat)ur|(Sun))day

In this mode, Sun and Sat are stored in backward reference 1.

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.