Peter Bengtsson wrote: > 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'.
> 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? I think sub() is more appropriate than finditer() for your problem, e. g.: >>> def process(match): ... return "_%s_" % match.group(1).title() ... >>> re.compile("(peter)", re.I).sub(process, "Peter Bengtsson PETER, or PeTeR") '_Peter_ Bengtsson _Peter_, or _Peter_' >>> Peter -- http://mail.python.org/mailman/listinfo/python-list