matching multiple regexs to a single line...

John Hunter jdhunter at nitace.bsd.uchicago.edu
Tue Nov 12 17:55:08 EST 2002


  Trent> A slight mod on John's code makes it seem pretty clean to me:
  Trent> 
  Trent> patterns = [re.compile('...'),
  Trent>             re.compile('...')]

That does look nicer, but doesn't allow for differential processing
depending on which re instance matched.  To do that, you can pair the
regexs with functions in a tuple of (rgx,action) pairs, or a dict if
order doesn't matter.  Admittedly the code below is more verbose than
the perl version, but at least the design is clean. 

import re

r1 = re.compile('John (\w+) (.*)')
r2 = re.compile('Bill')
r3 = re.compile('Sally (.*)')

def func2(m):
   return 'just a func'

actions = ((r1 , lambda m: 'I found ' + m.group(2)),
           (r2 , func2), 
           (r3 , lambda m: m.group(1)), 
           )

s1 = 'Sally was here'
s3 = 'John was Bill'
s5 = 'Bill and sally'
s2 = 'John was here'
s4 = 'No chance in hell'

lines = (s1,s2,s3,s4,s5)

for line in lines:
   for (rgx, func) in actions:
      m = rgx.match(line)
      if m:
         print func(m)
         break
   else:
      print 'Nobody matched line: %s' % line





More information about the Python-list mailing list