regexpr. and metachars

Alex Martelli aleaxit at
Thu Nov 2 00:05:55 CET 2000

"Asle Pedersen" <asle at> wrote in message
news:3A008C3A.E2812DE3 at
> I want to match strings containing regular expressions (regular expr.
> metachars) with regular expressions!! e.g. what regular expression would
> match "\W+" or  "\W+\w{1,2}" ???

>>> import re
>>> x=r'\W+'
>>> re.escape(x)
>>> m=re.compile(re.escape(x))
>>> m.match(x)
<SRE_Match object at 01051040>
>>> m.match('foo')

Got it?  Using the raw-string lets you write things out plainly
as opposed to
(warning -- doesn't work for odd ending numbers of \ ...!).

Using re.escape produces a version of this string, with a
further \ everywhere (as needed for re syntax), so that
no 'magic-characters' are left (only that very string will
be matched -- as you desire).  Don't be confused by the
fact that it's displayed in regular string representation (so
every backslash gets doubled...).

re.compile on that gives us the compiled regular expression
that only matches the original string.


More information about the Python-list mailing list