checking a string against multiple patterns

tomasz tmkmarc at googlemail.com
Tue Dec 18 07:41:47 EST 2007


Hi,

here is a piece of pseudo-code (taken from Ruby) that illustrates the
problem I'd like to solve in Python:

str = 'abc'
if str =~ /(b)/     # Check if str matches a pattern
  str = $` + $1    # Perform some action
elsif str =~ /(a)/ # Check another pattern
  str = $1 + $'    # Perform some other action
elsif str =~ /(c)/
  str = $1
end

The task is to check a string against a number of different patterns
(containing groupings).
For each pattern, different actions need to be taken.

In Python, a single match of this kind can be done as follows:

str = 'abc'
match = re.search( '(b)' , str )
if match: str = str[0:m.start()] + m.group(1)    # I'm not sure if
this way of accessing 'pre-match'
                                                               # is
optimal, but let's ignore it now

The problem is that you you can't  extend this example to multiple
matches with 'elif'
because the match must be performed separately from the conditional.

This obviously won't work in Python:

if match=re.search( pattern1 , str ):
  ...
elif match=re.search( pattern2 , str ):
  ...

So the only way seems to  be:

match = re.search( pattern1 , str ):
if match:
   ....
else:
   match = re.search( pattern2 , str ):
   if match:
      ....
   else:
       match = re.search( pattern3 , str ):
       if match:
           ....

and we end up having a very nasty, multiply-nested code.

Is there an alternative to it? Am I missing something? Python doesn't
have special variables $1, $2 (right?) so you must assign the result
of a match to a variable, to be able to access the groups.

I'd appreciate any hints.

Tomasz










More information about the Python-list mailing list