mixing regular expression

Christian Tanzer tanzer at swing.co.at
Sun Mar 17 07:58:44 EST 2002


"Ian McMeans" <imcmeans at home.com> wrote:

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

>>> import re
>>> foo = re.compile("someregexcode")
>>> bar = re.compile("moreregexcode")
>>> big = re.compile("%s|%s" % (foo.pattern, bar.pattern))
>>> big.pattern
'someregexcode|moreregexcode'

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list