Passing a function to re.sub in Python
I have strings which contain a number somewhere in them and I'm trying to
replace this number with their word notation (ie. 3 -> three). I have a
function which does this. The problem now is finding the number inside the
string, while keeping the rest of the string intact. For this, I opted to
use the re.sub function, which can accept a "callable". However, the
object passed to it is the internal _sre.SRE_Match and I'm not sure how to
handle it. My function accepts a number or its string representation.
How should I write some helper function which can be used to bridge the
re.sub call with my function doing the required processing? Alternatively,
is there a better way to do what I want?
No comments:
Post a Comment