How to insert string in each match using RegEx iterator
504crank at gmail.com
504crank at gmail.com
Wed Jun 10 00:13:33 EDT 2009
By what method would a string be inserted at each instance of a RegEx
match?
For example:
string = '123 abc 456 def 789 ghi'
newstring = ' INSERT 123 abc INSERT 456 def INSERT 789 ghi'
Here's the code I started with:
>>> rePatt = re.compile('\d+\s')
>>> iterator = rePatt.finditer(string)
>>> count = 0
>>> for match in iterator:
if count < 1:
print string[0:match.start()] + ' INSERT ' + string[match.start
():match.end()]
elif count >= 1:
print ' INSERT ' + string[match.start():match.end()]
count = count + 1
My code returns an empty string.
I'm new to Python, but I'm finding it really enjoyable (with the
exception of this challenging puzzle).
Thanks in advance.
More information about the Python-list
mailing list