using python to do perl-like text manipulation
April 7th, 2009 by Lawrence David
to do string substitutions in python, use the “re” module and the “sub” function:
>>> import re
…
>>> test = “2545423/1-4242:0.3245″
>>> test ; re.sub(“/\d-\d*:”,”:”,test)
’2545423/1-4242:0.3245′
’2545423:0.3245′
and, to match a group and then use it in the substitution, use parentheses and the \<number> operation:
>>> new_boot_s = re.sub(“(\d+:)”,”n\\1″,boot_s)