Regular Expressions to Replace Strings

piet at cs.uu.nl piet at cs.uu.nl
Wed May 16 11:15:56 EDT 2001


>>>>> "Colin Meeks" <colinmeeks at home.com> (CM) writes:

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

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

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

You have no group. To get one, use the r.e. "(python)".
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl



More information about the Python-list mailing list