On 8/05/2006 10:31 PM, Mirco Wahab wrote:
[snip]
>
> 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):
What do you expect when t == "green socks and red shoes"? Is it possible
that you mean to use search() rather than match()?
> 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 regex in advance in Python, either.
Please consider the module-level function search() ...
if re.search(r"blue|white|red", t):
# also, no need for () in the regex.
--
http://mail.python.org/mailman/listinfo/python-list