regular expression - re module

Zoltan Nemeti zmeneti at chello.hu
Sun Dec 22 18:01:49 EST 2002


Thanks, John, this really helps.
However, it would be a nice thing to create the re.

Zoltan

John Hunter wrote:

>>>>>>"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