Maybe I should implement those two things.
Note: There is no whitespace between * and MARK, but if I remove it HN formats it as an italic string.
Given `(?<g1>Sat)urday|(?<g2>Sun)day`, and a successful match, either `$+{g1}` or `$+{g2}` will hold the relevant subpart.
Quickie demo script:
my $re = "(?<g1>Sat)urday|(?<g2>Sun)day";
foreach my $d (qw(Friday Sunday Thursday Saturday)) {
my $out = '<default>';
if ($d =~ /$re/) {
if ($+{g1}) { $out = 'Saturday' }
elsif ($+{g2}) { $out = 'Sunday' }
}
print "$d routes to $out\n";
}Perl 5.10 from 2007 supports named captures. (Google confirmed that.)
Edit: nikic in the parallel comment is correct, named captures are affected the same way in Perl's regexp engine with ?|. (Google perlre and search for ?|.)
As lmm points out (I think), one can get a big advantage precisely by not requiring the regex code to know this. (Regexes are 'really' DFAs, and a DFA that makes it to its start state not just does not but can not know how it got there.) The more power one gives regexes, the less, well, regular they are, and the slower they become to match.
You'd have to compile the DFA differently, of course. A really simple version would just compile each alternation into its own DFA separately, and then chain them together with "try each in sequence". That's still a DFA, so it's not a matter of a different class of computations, though it's often slower by a constant factor (due to not being able to share any information between the alternations when matching them).
https://github.com/darius/sketchbook/blob/master/lex/dfa_nfa...
P.S. I might as well mention that I obviously meant to refer to a DFA reaching its end, rather than start, state.
http://stackoverflow.com/questions/3287860/what-is-a-tagged-...
~^(?|
xx(x)/user/([^/]+)/(\d+)
| x(xx)/user/(\d+)
| (xxx)/user/([^/]+)
)$~x
Then just prepend 'xxx' to the front of uri in the call to preg_match. Now the second entry in matches tells you which handler, so for example for uri = '/user/nikic/42' you pass 'xxx/user/nikic/42' and get back 'xxx/user/nikic/42', 'x', 'nikic', and '42' where that 'x' tells you it's the first handler.I'm not an expert in how php does REs but I imagine there may be a way to use preg_replace or similar to replace the first '/' in the uri with 'x', 'xx', or 'xxx' and still get a the groups returned for the matches for user and id say. This could save a potentially costly string creation.
There's a common misconception that regular expressions are slow, but in many cases they offer much better performance than a manual implementation with lots of strpos etc. In the end PCRE does the same thing, but in C and using a JIT compiler. Apart from very simple cases you don't have a chance to keep up with that using PHP code.