matching multiple regexs to a single line...

Alexander Sendzimir lists at battleface.com
Tue Nov 12 15:04:31 EST 2002


How does one match multiple regexs to a single line as in a compiler in
Python?  I'm used to doing the following in Perl, for example:

$line = <>;

if ( $line ~= /regex1/ )
{...}
elsif ( $line ~= /regex2/ )
{...}
elsif (...)
{ etc... }

To do this in Python and be able to get at the match if a regex succeeds
means I have to break up the if's because I haven't been able to get
Python to make an assignment in an if-statement. In other words,

if ( myMatch = re1.match( line ) ) :
    ...
elif ( myMatch = re2.match( line ) ) :
    ...

doesn't work for me. Perhaps I've missed something simple.

Thanks,

Alex




More information about the Python-list mailing list