Problem in Using re.subn()

Joseph C. Kopec kopecjc at
Fri Feb 18 07:34:45 EST 2000

I was working through an example in Preston Lander's "Writing CGI
Scripts in Python" on DevShed
( -- an excellent
article, which I highly recommend -- and kept getting an error message
when attempting to run a script set forth therein containing re.subn(). 
Basically, the re.subn() call looks like this:

def Display(Content):
    TemplateHandle = open(TemplateFile, "r")  # open in read only mode
    # read the entire file as a string
    TemplateInput = 
    TemplateHandle.close()                    # close the file

    # this defines an exception string in case our
    # template file is messed up
    BadTemplateException = "There was a problem with the HTML template."

    SubResult = re.subn('<!-- *** INSERT CONTENT HERE *** -->', Content,
    if SubResult[1] == 0:
        raise BadTemplateException

    print "Content-Type: text/html\n\n"
    print SubResult[0]

Which gives a Trackback of:

Traceback (innermost last):
  File "/usr/local/apache/cgi-bin/", line 100, in ?
  File "/usr/local/apache/cgi-bin/", line 38, in DisplayForm
  File "/usr/local/apache/cgi-bin/", line 23, in Display
    SubResult = re.subn('<!-- *** INSERT CONTENT HERE *** -->', Content,
  File "/var/tmp/python-root/usr/lib/python1.5/", line 52, in subn
    pattern = _cachecompile(pattern)
  File "/var/tmp/python-root/usr/lib/python1.5/", line 33, in
    value = compile(pattern, flags)
  File "/var/tmp/python-root/usr/lib/python1.5/", line 79, in
    code=pcre_compile(pattern, flags, groupindex)
pcre.error: ('nothing to repeat', 6)

I know my Content and TemplateInput values are good strings.  Does
anyone have any guess what is going on?  Any suggestions would be much

More information about the Python-list mailing list