What is the non-regex equivalent of subn? I would like to replace a pattern in a string and count how many times was the pattern replaced.
I ended up with:
def replacen(pat, repl, txt):
txt2 = txt.replace(pat, repl)
if (len(pat) != len(repl)):
return (txt2, (len(txt) - len(txt2)) / (len(pat) - len(repl)))
else:
return (txt2, txt.count(pat))
Is there a more elegant solution?