php—PCRE regular expression conditional subgroup

伊谢尔伦
Release: 2016-11-21 17:12:26
Original
1231 people have browsed it

You can make the matcher conditionally match a subgroup or select between two optional subgroups based on the result of an assertion or whether a previous capturing subgroup matches. The two syntaxes for conditional subgroups are as follows:

(?(condition)yes-pattern)
(?(condition)yes-pattern|no-pattern)
Copy after login

If the condition is met, use yes-pattern, otherwise use no-pattern (if specified). If there are more than 2 optional subgroups, a compile-time error will be generated.

There are two conditions. If the parentheses of (condition) are text consisting of numbers, the condition is satisfied when the (previous) subgroup represented by the number is matched (that is, using the yes-pattern). Consider the following pattern, which has some whitespace added to make it easier to read (see PCRE_EXTENDED option) and divided into three parts: ( ( )? [^()]+ (?(1) ) )

pattern The first part of matches an optional left parenthesis, and if that character occurs, sets it to the first subgroup of the captured substring. The second part matches one or more non-bracket characters. The third part is a conditional subgroup, which will test whether the first subgroup matches. If it matches, that is to say, the target string starts with a left bracket and the condition is TRUE, then use yes-pattern, which means that a match needs to be performed here. Right bracket. Otherwise, since no-pattern is not present, the subgroup does not match anything. In other words, this pattern matches a sequence of characters without parentheses or enclosed by closing parentheses.

If conditional string (R), it is satisfied when getting a recursive call to a pattern or subpattern. At the "top level", the condition is always false.

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

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

Conditional A positive assertion that matches an optional sequence of non-lowercase alphabetic characters followed by a lowercase letter. In other words, it tests for the presence of at least one lowercase letter in the target, and if a lowercase letter is found, the target matches the first optional branch, otherwise 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.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!