mixing regular expression

Ian McMeans imcmeans at home.com
Sun Mar 17 01:56:23 EST 2002


"Peter Hansen" <peter at engcorp.com> wrote in message
news:3C943641.CD0086B3 at engcorp.com...
> 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

Yeah, I think I'll end up joining the pattern strings. What I wanted to do
was create a new regular expression using the regular expressions that I've
already made, without doing it via string concatenation -- which, in
hindsight, isn't actually that hard to do.

Ideally, in my example above,
foo = re.compile("someregexcode")
bar = re.compile("moreregexcode")
 big = re.compile("foo|bar")

would be equivalent to

big = re.compile("(someregexcode)|(moreregexcode)")

I wanted to do it that way so that I could make one regex at a time, test
it, and then include it in the larger expression.





More information about the Python-list mailing list