In Python, how do I do a string replacement AND retrive the replaced substring?

Posted by mike on Stack Overflow See other posts from Stack Overflow or by mike
Published on 2010-03-19T21:58:07Z Indexed on 2010/03/19 22:01 UTC
Read the original article Hit count: 198

Filed under:
|

In Perl, I would write:

$x = "abbbc";
$x =~ s/(b+)/z/;
print "Replaced $1 and ended up with $x\n";
# "Replaced bbb and ended up with azc"

How do I do this in Python -- do a regular-expression string replacement and record what it was that got replaced?

© Stack Overflow or respective owner

Related posts about python

Related posts about regex