re module substitution confusion

Doug Farrell writeson at
Mon Jul 7 23:26:23 CEST 2003

Hi all,

I'm trying to do the following from within a code module:

import re

# text to match
text = "Good morning x something /x, how are you today x something
else /x"

# pattern to match
regex = re.compile("(x)(.*?)(/x)", re.DOTALL)

# find first match
m =, text)

# keep looking while there are matches
while m != None:
    # substitute in some other text for the exact match
    text = re.sub(, "Mr. Phelps", text)

    # find the next match
    m =, text)

print text

This works within the Python shell, but I can't seem to make it work
from within a program. It would seem like the re.sub(
would work fine as just returns the matched string

Any help would be greatly appreciated,
Doug Farrell

More information about the Python-list mailing list