Regular Expressions to Replace Strings

Colin Meeks colinmeeks at home.com
Wed May 16 09:58:39 EDT 2001


I'm trying to open a text file and do a search and replace that is case
insensitive and replaces all occurances.  I have the following code, but it
has problems if the word exists in the string.  Does anybody have any ideas
what I'm doing wrong.

PageRoot="../pages/contactus.ptm"
UseRoot=open(PageRoot, "r").read()
UseWord="python"
pat=re.compile(UseWord,re.I)
UseRoot=pat.sub('<B>\\1</B>', UseRoot)

The error I get is unknown group number.    Thanks in advance. I want to use
this code as part of my search engine, so the word will always be a
variable.

Colin





More information about the Python-list mailing list