Squezing in replacements into strings
Peter Bengtsson
mail at peterbe.com
Mon Apr 25 07:07:26 EDT 2005
I've got a regular expression that finds certain words from a longer string.
>From "Peter Bengtsson PETER, or PeTeR" it finds: 'Peter','PETER','PeTeR'.
What I then want to do is something like this:
def _ok(matchobject):
# more complicated stuff happens here
return 1
def _massage(word):
return "_" + word + "_"
for match in regex.finditer(text):
if not _ok(match):
continue
text = text[:match.start()] +\
_massageMatch(text[match.start():match.end()]) +\
text[match.end():]
This code works and can convert something like "don't allow the fuck swear word"
to "don't allow the _fuck_ swear word".
The problem is when there are more than one matches. The match.start() and
match.end() are for the original string but after the first iteration in the
loop the original string changes (it gains 2 characters in length due to the
"_"'s)
How can I do this this concatenation correctly?
--
Peter Bengtsson,
work www.fry-it.com
home www.peterbe.com
hobby www.issuetrackerproduct.com
More information about the Python-list
mailing list