Iteration within re.sub()?
Bryant Huang
735115 at gmail.com
Mon Dec 13 21:38:18 EST 2004
Hi,
Is it possible to perform iteration within the re.sub() function call?
For example, if I have a string like:
str = "abbababbabbaaa"
and I want to replace all b's with an integer that increments from 0,
could I do that with re.sub()?
Replacing b's with 0's is trivial:
i = 0
pat = re.compile("b")
print pat.sub(`i`, str)
Now, how can I increment i in each replacement? Is this possible? Like,
using a lambda function, for example? Or do I use a different re
function altogether?
I use this trivial [ab]* language for simplicity, but I have a real
problem where I need to match a more complex regex and replace it with
an incrementing integer.
Thanks so much!
Bryant
More information about the Python-list
mailing list