Is it possible to group parts of a regular expression WITHOUT using parentheses? Or, in other words, not match certain parenthetical statements? Here’s what I mean–consider the following regular expression:
$pattern = '/^(what|where|who) (are|is) (.+)$/i';
preg_match($pattern, $input, $matches);
NOW. Notice the first two subqueries–the ones with the vertical bars. How do I prevent those from coming up? If $input is “who is he”, I want “he” to be the only subquery that is matched.
I understand that, in this example, I could just grab $matches[3] and that’d give me the correct result. However, due to the nature of my application, I am dynamically generating regular expressions, and the structure will not always be like I have it in this example. I could have X number of vertical bar-delimited subqueries, and Y number of (.+) subqueries. I only want to match the (.+) ones.
Is it possible to do this? I’m okay with rewriting my regular expression if necessary, but I can’t figure out how to group statements without using parentheses, which mark subqueries. I’m using PHP, if it matters.
Thanks!
You can eliminate backreferences in the first two groupings like so:
The
?:after the opening round bracket tells the regex engine not to create a backreference of this grouping. It’s good to include for optimization whenever you don’t plan on using the backreference.