Doing both regex match and assignment within a If loop?

Alain Ketterlin alain at dpt-info.u-strasbg.fr
Fri Mar 29 11:06:30 CET 2013


Victor Hooi <victorhooi at gmail.com> 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).

> Is it possible to somehow test for a match, as well as do assignment
> of the re match object to a variable?

Yes, use '...(...)...' and MatchObject.group(). See the other messages.

-- Alain.



More information about the Python-list mailing list