re.subn error - please help

A

Ajay

hi!

i have a template file with the string
"<!-- *** INSERT CONTEXT DEF BOX *** -->"

in a main function i have
import cgi
import re
import utilities

# specify the filename of the template file
templateFile = "templates/template.html"
contextTemplateFile = "templates/contextTemplate.html"


templateHandle = open(templateFile, "r") # open in read only mode
# read the entire file as a string
templateInput = templateHandle.read()
templateHandle.close() # close the file


templateHandle = open(contextTemplateFile, "r") # open in read only mode
# read the entire file as a string
contextTemplateInput = templateHandle.read()
templateHandle.close() # close the file

subResult = re.subn("<!-- *** INSERT CONTEXT DEF BOX *** -->",
contextTemplateInput, templateInput)
if subResult[1] == 0:
raise badTemplateException

however when i run this i get an error saying:
File "index.cgi", line 24, in ?
subResult = re.subn("<!-- *** INSERT CONTEXT DEF BOX *** -->",
contextTempla
teInput, templateInput)
File "/usr/local/lib/python2.3/sre.py", line 151, in subn
return _compile(pattern, 0).subn(repl, string, count)
File "/usr/local/lib/python2.3/sre.py", line 230, in _compile
raise error, v # invalid expression
sre_constants.error: multiple repeat

please help

thanks
 
H

Helmut Jarausch

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top