Iteration within re.sub()?

Robert Brewer fumanchu at amor.org
Tue Dec 14 04:05:15 CET 2004


Bryant Huang wrote:
> 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.

It's possible. Here's a quick, ugly version, which:

1. Uses a function for the "repl" arg of re.sub().
2. Uses a list to get around i being local inside of sub(). You could
just as well use the global statement.
3. Starts i[0] at -1. You could also use itertools.count or range to
good effect.

PythonWin 2.3.2 (#49, Oct  2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)]
on win32.
>>> import re
>>> s = "abbababbabbaaa"
>>> i[0] = -1
>>> def sub(match):
... 	i[0] += 1
... 	return str(i[0])
... 
>>> re.sub("b", sub, s)
'a01a2a34a56aaa'


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list