mixing regular expression

Peter Hansen peter at engcorp.com
Sun Mar 17 01:22:57 EST 2002


Ian McMeans wrote:
> 
> How should go I about nesting regular expressions in python?
> 
> I want one huge regular expression to be composed of a bunch of others - is
> there some way I can include a regular expression inside another?
> 
> Or should I do it with strings - just save each regular expression's string,
> and then compile each combination separately?
> 
> What I mean is this:
> foo = re.compile("someregexcode")
> bar = re.compile("moreregexcode")
> 
> big = re.compile("foo|bar")
> 
> is that possible at all? To combine regular expression objects? Or do you
> suggest that I just make strings for foo and bar, and then compile the
> combined strings into big?

It seems to me you need to explain whether you want them concatenated
(in which case can't you just join the pattern strings?), or ORed
together (similar to the foo|bar case you give above?), or "nested"
(which to me implies some kind of hierarchy) or maybe just applied
sequentially (in which case just do that?).

-Peter



More information about the Python-list mailing list