re.match

Fredrik Lundh fredrik at pythonware.com
Wed Mar 21 18:53:10 CET 2001


Bob Cannard wrote:
> Another possibility would have been to combine compiled regular
> expressions, but there doesn't seem to be a way to do this. Does
> anyone know of a function or piece of magic that I've missed?

not really.  you can make it look like you're reusing the old
pattern, but it doesn't save you any time:

>>> import re
>>> p1 = re.compile("foo")
>>> p1.pattern
'foo'
>>> p2 = re.compile(p1.pattern + "$")

>>> print p1.match("foo")
<SRE_Match object at 008CCB00>
>>> print p1.match("foobar")
<SRE_Match object at 008CCD90>
>>> print p2.match("foo")
<SRE_Match object at 008CCB00>
>>> print p2.match("foobar")
None

Cheers /F





More information about the Python-list mailing list