Doing both regex match and assignment within a If loop?

Arnaud Delobelle arnodel at gmail.com
Fri Mar 29 06:41:26 EDT 2013


On Friday, 29 March 2013, Alain Ketterlin wrote:

> Victor Hooi <victorhooi at gmail.com <javascript:;>> writes:
>
> >     expression1 = re.compile(r'....')
> >     expression2 = re.compile(r'....')
> [...]
>
> Just a quick remark: regular expressions are pretty powerful at
> representing alternatives. You could just stick everything inside a
> single re, as in '...|...'
>
>
Then use the returned match to check which alternative was recognized
> (make sure you have at least one group in each alternative).
>
>
Yes, and for extra ease/clarity you can name these alternatives (
'(?P<name>pattern)').  Then you can do

if m.group('case1'):
    ...
elif m.group('case2'):
   ...

-- 
Arnaud
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130329/b275484d/attachment.html>


More information about the Python-list mailing list