Python's regular expression?

Mirco Wahab peace.is.our.profession at gmx.de
Mon May 8 15:24:49 CEST 2006


Hi Duncan

> There is no need to compile the regular expression in advance in Python 
> either:
> ... 
> The only advantage to compiling in advance is a small speed up, and most of 
> the time that won't be significant.

I read 'some' introductions into Python Regexes
and got confused in the first place when to use
what and why.

After some minutes in this NG I start to get
the picture. So I narrowed the above regex-question
down to a nice equivalence between Perl and Python:

Python:

   import re

   t = 'blue socks and red shoes'
   if re.match('blue|white|red', t):
       print t

   t = 'blue socks and red shoes'
   if re.search('blue|white|red', t):
      print t

Perl:

   use Acme::Pythonic;

   $t = 'blue socks and red shoes'
   if $t =~ /blue|white|red/:
     print $t


And Python Regexes eventually lost (for me) some of
their (what I believed) 'clunky appearance' ;-)

Thanks

Mirco



More information about the Python-list mailing list