Regular expression : non capturing groups are faster ?

Octavian Rasnita orasnita at gmail.com
Tue Jan 3 06:59:27 EST 2012


From: "candide" <candide at free.invalid>
Subject: Regular expression : non capturing groups are faster ?


Excerpt from the Regular Expression HOWTO 
(http://docs.python.org/howto/regex.html#non-capturing-and-named-groups) :


-----------------------------------------------
It should be mentioned that there’s no performance difference in 
searching between capturing and non-capturing groups; neither form is 
any faster than the other.
-----------------------------------------------


Now from the Perl Regular Expression tutorial 
(http://perldoc.perl.org/perlretut.html#Non-capturing-groupings) :


-----------------------------------------------
Because there is no extraction, non-capturing groupings are faster than 
capturing groupings.
-----------------------------------------------


The second assertion sounds more likely. It seems very odd that Python 
and Perl implementations are divergent on this point. Any opinion ?


-- 
**
At least in Perl's case, it is true. I tested and using (?:...) is much faster than ().

Of course, it takes a few seconds for dozen million matches...

Octavian





More information about the Python-list mailing list