[Catalog-sig] Help with Python Regex's

Guido van Rossum guido@digicool.com
Wed, 16 May 2001 22:59:56 -0500


> import re
> 
> f = open("C:\\www.timemanager.com\\test.html")
> fileContent = f.read()
> f.close
> 
> newVal = "Heres the replacement code which means it worked"
> key = "Replace Me"
> 
> re.sub('\<!--\s*' + key + '\s*--\>',newVal,fileContent)

Try

  fileContent = re.sub(...)

instead.  Python never has side effects on simple variables!

> print fileContent
> 
> raw_input("ok done")

--Guido van Rossum (home page: http://www.python.org/~guido/)