[Tutor] weird error messge
dyoo at hkn.eecs.berkeley.edu
Sun Nov 2 17:55:57 EST 2003
> Hey can anyone help me. Whenever i run this script i get an error i cant
> make anything of. Can someone explain it to me??
The error message:
> traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "retrieveposts.py", line 32, in retrieve_posts
> posts = re.sub('<!--***INSERT NAME HERE***-->', post, posts)
> File "/usr/lib/python2.2/sre.py", line 143, in sub
> return _compile(pattern, 0).sub(repl, string, count)
> File "/usr/lib/python2.2/sre.py", line 229, in _compile
> raise error, v # invalid expression
> sre_constants.error: multiple repeat
is important: the system is saying that the regular expression is using an
illegal "multiple repeat" thing that it has no idea how to handle.
What it's really trying to say is that '*' is a regular expression
metacharacter. For example,
represents any number of "a" characters. But what does something like:
mean? To repeat it more? It turns out that this isn't legal as a regular
Traceback (most recent call last):
File "<stdin>", line 1, in ?
line 179, in compile
return _compile(pattern, flags)
line 229, in _compile
raise error, v # invalid expression
sre_constants.error: multiple repeat
So that explains the error message. How do we fix things? Looking back,
it appears like we want to match for the literal character sequence:
'<!--***INSERT NAME HERE***-->'
but we need to teach the regex engine not to treat stars as special! To
fix the regular expression, we need to "escape" those stars so that
they're treated as literal characters instead of metacharacters. And the
function re.escape() does this for us:
>>> s = '<!--***INSERT NAME HERE***-->'
>>> import re
'\\<\\!\\-\\-\\*\\*\\*INSERT\\ NAME\\ HERE\\*\\*\\*\\-\\-\\>'
>>> print re.escape(s)
\<\!\-\-\*\*\*INSERT\ NAME\ HERE\*\*\*\-\-\>
It does look ugly, but that's ok, since only the regex engine will be
seeing it. Notice that re.escape() puts backslashes right before each
star --- that's the way we can protect characters from being interpreted
Please feel free to ask more questions on thes tuff that doesn't make
sense yet. Hope this helps!
More information about the Tutor