[newbie] Strange behavior of the re module

Hans Nowak hans at zephyrfalcon.org
Sat Aug 21 00:30:04 EDT 2004


Fred wrote:

> stuff="\colortbl\red0\n0"
> 
> template = "BLA"
> 
> template = re.sub('BLA', stuff, template)
> --------------------------------
> 
> => It appears that the re module isn't very friendly with backslashes,
> at least on the Windows platform. Does someone know why, and what I
> could do, since I can't rewrite the source HTML documents that contain
> backslashes.

It's not the re module, it's that backslashes have special meaning in string 
literals.  See also:

   http://docs.python.org/tut/node5.html#SECTION005120000000000000000

   http://docs.python.org/ref/strings.html

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"

HTH,

--
Hans Nowak (hans at zephyrfalcon.org)
http://zephyrfalcon.org/




More information about the Python-list mailing list