> Is there some weirdness with alternations and anchors?
It's a feature called "trie optimization"¹ which only works for literal characters and can't optimize meta symbols like anchors.
> (I could rewrite ^aaa|^bbb|^ccc as ^(aaa|bbb|ccc) , but it might be that only some of the inputs are anchored.)
So make two or-lists one with preceding anchor, one without and or them. Both will be trie-optimized and this should be max speed again.
Like:
((?:^(?:aaa|bbb|ccc))|(?:ddd|eee|ccc))
*untested!!!*
(?:...) is for grouping without matching capturing.
(Not sure if the one around the anchored part is necessary... I don't think so, just playing safe)
Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery
¹) compare Re^2: Looking for a cleaner regex ( trie since 5.10 ! )
Tested in perldebugger
perl -de0 ... DB<11> @matches = ( "aaa x ddd y ccc" =~ /(^ (?:aaa|bbb|ccc) | (?:dd +d|eee|ccc) )/xg ); DB<12> print "@matches" aaa ddd ccc DB<13>
In reply to Re: Alternations and anchors (trie optimization)
by LanX
in thread Alternations and anchors
by Chuma
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |