Python's regular expression?
duncan.booth at invalid.invalid
Mon May 8 15:04:38 CEST 2006
Mirco Wahab wrote:
> Lets see - a really simple find/match
> would look like this in Python:
> import re
> t = 'blue socks and red shoes'
> p = re.compile('(blue|white|red)')
> if p.match(t):
> print t
> which prints the text 't' because of
> the positive pattern match.
> In Perl, you write:
> use Acme::Pythonic;
> $t = 'blue socks and red shoes'
> if ($t =~ /(blue|white|red)/):
> print $t
> which is one line shorter (no need
> to compile the regular expression
> in advance).
There is no need to compile the regular expression in advance in Python
t = 'blue socks and red shoes'
if re.match('(blue|white|red)', t):
The only advantage to compiling in advance is a small speed up, and most of
the time that won't be significant.
More information about the Python-list