re module substitution confusion

Doug Farrell writeson at earthlink.net
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 = re.search(regex, text)

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

    # find the next match
    m = re.search(regex, 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(m.group()...)
would work fine as m.group() just returns the matched string
completely.

Any help would be greatly appreciated,
Doug Farrell




More information about the Python-list mailing list