RegEx issues

Roy Smith roy at panix.com
Sat Jan 24 13:55:48 EST 2009


Sean Brown <sbrown.home@[spammy]gmail.com> wrote:

> The problem is it appears that python is escaping the \ in the regex
> because I see this:
> >>>reg = '\[\[(.*)\]\];'

The first trick of working with regexes in Python is to *always* use raw 
strings.  Instead of

reg = '\[\[(.*)\]\];'

you want

reg = r'\[\[(.*)\]\];'

In this case, I think it ends up not mattering, but it's one less thing to 
worry about.  Next, when looking at something like

> >>> reg
> '\\[\\[(.*)\\]\\];'

it's hard to see exactly what all the backslashes mean.  Which are real and 
which are escapes?  Try doing

>>> print reg
\[\[(.*)\]\];

which gets you the str(reg) instead of repr(reg).  Another trick when 
you're not 100% what you're looking at is to explode the string like this:

>>> [c for c in reg]
['\\', '[', '\\', '[', '(', '.', '*', ')', '\\', ']', '\\', ']', ';']



More information about the Python-list mailing list