Compare regular expressions

Diez B. Roggisch deets at nospam.web.de
Tue Apr 17 10:17:54 CEST 2007


Paddy schrieb:
> On Apr 16, 10:50 am, Thomas Dybdahl Ahle <lob... at gmail.com> wrote:
>> Hi, I'm writing a program with a large data stream to which modules can
>> connect using regular expressions.
>>
>> Now I'd like to not have to test all expressions every time I get a line,
>> as most of the time, one of them having a match means none of the others
>> can have so.
>>
>> But ofcource there are also cases where a regular expression can
>> "contain" another expression, like in:
>> "^strange line (\w+) and (\w+)$" and "^strange line (\w+) (?:.*?)$" in
>> which case I'd like to first test the seccond and only if it mathces test
>> the seccond.
>>
>> Do anybody know if such a test is possible?
>> if exp0.contains(exp1): ...
> 
> you could OR all the individual RE's test them all at once then find
> out which matched.
> 
> big_re = "|".join( r"(?P<__match_%i__>%s)" % (i, r)
>                    for i,r in enumerate(regexps) )
> 
> now if one of the regexps matches then the corresponding named
> group should have a non-empty string value.

This doesn't answer the question if two of the sub-expressions matched.

Diez



More information about the Python-list mailing list