regular expression - re module

John Hunter jdhunter at ace.bsd.uchicago.edu
Fri Dec 20 17:01:21 EST 2002


>>>>> "Zoltan" == Zoltan Nemeti <zmeneti at chello.hu> writes:

    Zoltan> I'd like to use a general pattern, so only red|green
    Zoltan> should be specific. For my pleasure...


This is made easy by the fact that the pattern of interest is always
at the 3rd position from the end.  Split the names on '.' and test the
part at index -3 (negative indices count from the end).

Eg, 

import re
rgx = re.compile('red|green')

names = ('b.a.com', 'Z.Y.X.W.com', 'Z.Y.X.red.W.com',
         'b.green.a.com', 'Z.Y.X.heavy.W.com')

for name in names:
    parts = name.split('.')
    if len(parts)>=3 and rgx.match(parts[-3]): continue
    print '%s matched!' % name

John Hunter 




More information about the Python-list mailing list