[newbie] Strange behavior of the re module

Fred fred at acme.com
Sat Aug 21 00:42:50 EDT 2004


On Sat, 21 Aug 2004 00:30:04 -0400, Hans Nowak <hans at zephyrfalcon.org>
wrote:
>To use a non-escaping backslash in a string literal, use a double backslash:
>
>   stuff = "\\colortbl\\red0\\n0"
>
>or a raw string:
>
>   stuff = r"\colortbl\red0\n0"

Thx Hans for the prompt answer. I'll have to use the second form since
I can't modify the content of the HTML pages I'm looping through...
but no matter which option I use (either r or R), Python is still not
happy:

---------------------------------------
import re

#NOK
stuff=r"\colortbl\red0\gn0"
#NOK
stuff=R"\colortbl\red0\gn0"

template = "BLA"
template = re.sub('BLA', stuff, template)
---------------------------------------

Traceback (most recent call last):
  File "C:\test.py", line 9, in ?
    template = re.sub('BLA', stuff, template)
  File "G:\Python23\lib\sre.py", line 143, in sub
    return _compile(pattern, 0).sub(repl, string, count)
  File "G:\Python23\lib\sre.py", line 257, in _subx
    template = _compile_repl(template, pattern)
  File "G:\Python23\lib\sre.py", line 244, in _compile_repl
    raise error, v # invalid expression
sre_constants.error: bad group name

Maybe the r/R prefix is not available in ActivePython?

Thanks
Fred.



More information about the Python-list mailing list