简体   繁体   中英

Regular expressions - How to control whether parenthesis mark a sub group or do something else (PHP - preg_match)

How do I control whether parenthesis in my regular expression mark a sub group, do something else, or both?

For example if I have strings such as "AA12345" and "AB12345" and I want to preg_match for the first two letters which are always either AA or AB, I have:

preg_match('/(A(A|B)).*/',$string,$matches);

(I put the .* for the sake of this question because the rest of the string isn't relevant)

With this setup, assuming $string="AA12345", I'm getting $matches =

Array
(
[0] => AA12345
[1] => AA
[2] => A
)

I don't need or want the "[2] => A" as a result, but I can't remove the parenthesis from the regex because they are needed for the OR operator. How do I deal with this? Just ignore the result, or is there a better way?

You can use a "non-capturing group" of the form (?:...) :

preg_match('/(A(?:A|B)).*/',$string,$matches);

As the documentation puts it:

If an opening parenthesis is followed by "?:", the subpattern does not do any capturing, and is not counted when computing the number of any subsequent capturing subpatterns. For example, if the string "the white queen" is matched against the pattern the ((?:red|white) (king|queen)) the captured substrings are "white queen" and "queen", and are numbered 1 and 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM