/ Published in: Other
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import re def replace(match, withwhat): starttext = match.group(0) n = len(withwhat) off = match.start(0) tmp = starttext for i in range(n): delta = len(tmp)-len(starttext) tmp = tmp[:match.start(i+1)-off+delta ] + withwhat[i](match.group(i+1)) + tmp[match.end(i+1)-off+delta:] return tmp #test funcs = [lambda x: '1'+x+'1',lambda x: '2'+x+'2'] #regex contains 2 capturing groups print re.sub(regex, lambda x: replace(x, funcs), text)