Passing a function to re.sub in Python

后端 未结 3 977
囚心锁ツ
囚心锁ツ 2020-11-28 06:44

I have strings that 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 that does this. Th

3条回答
  •  借酒劲吻你
    2020-11-28 07:43

    To make your function fit with re.sub, you can wrap it with a lambda:

    re.sub('pattern', lambda m: myfunction(m.group()), 'text')
    

提交回复
热议问题