Feed on

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)

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)

Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

More blogs about http://desk.stinkpot.org:8080/tricks.