Python's regular expression?

Duncan Booth duncan.booth at invalid.invalid
Mon May 8 17:15:16 CEST 2006


Nick Craig-Wood wrote:

> Which translates to
> 
>   match = re.search('(blue|white|red)', t)
>   if match:
>      print "Colour:", match.group(1)
>   else:
>      match = re.search('(socks|tights)', t)
>      if match:
>         print "Garment:", match.group(1)
>      else:
>         match = re.search('(boot|shoe|trainer)', t)
>         if match:
>            print "Footwear:", match.group(1)
>            # indented ad infinitum!

This of course gives priority to colours and only looks for garments or 
footwear if the it hasn't matched on a prior pattern. If you actually 
wanted to match the first occurrence of any of these (or if the condition 
was re.match instead of re.search) then named groups can be a nice way of 
simplifying the code:

PATTERN = '''
    (?P<c>blue|white|red)
|   (?P<g>socks|tights)
|   (?P<f>boot|shoe|trainer)
'''
PATTERN = re.compile(PATTERN, re.VERBOSE)
TITLES = { 'c': 'Colour', 'g': 'Garment', 'f': 'Footwear' }

match = PATTERN.search(t)
if match:
    grp = match.lastgroup
    print "%s: %s" % (TITLES[grp], match.group(grp))

For something this simple the titles and group names could be the same, but 
I'm assuming real code might need a bit more.



More information about the Python-list mailing list