re.subn error - please help

Helmut Jarausch jarausch at igpm.rwth-aachen.de
Mon Jun 28 11:57:35 CEST 2004


Ajay wrote:
...
> subResult = re.subn("<!-- *** INSERT CONTEXT DEF BOX *** -->", 
> contextTemplateInput, templateInput)

The first argument to subn is not a simple string but a regular
expression where many characters have special meaning like the star
here, the the re-module documentation.
If you want to plugin a general string as string then you have to
escape all characters with special meaning before calling subn.
You can do this with the 'escape' function from the re module.
e.g.
subResult = re.subn(re.escape(
   "<!-- *** INSERT CONTEXT DEF BOX *** -->"),
   contextTemplateInput, templateInput)

But if you don't need the power (and complexity) of regular expressions,
I'd prefer the 'replace' function of the 'string' module, e.g.
subResult = templateInput.replace(
   "<!-- *** INSERT CONTEXT DEF BOX *** -->", contextTemplateInput)

Helmut.

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany



More information about the Python-list mailing list