[newbie] Strange behavior of the re module

Hans Nowak hans at zephyrfalcon.org
Sat Aug 21 06:30:04 CEST 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:



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"


Hans Nowak (hans at zephyrfalcon.org)

More information about the Python-list mailing list