Passing a function to re.sub in Python

You should call group() to get the matching string:

import re

number_mapping = {'1': 'one',
                  '2': 'two',
                  '3': 'three'}
s = "1 testing 2 3"

print re.sub(r'\d', lambda x: number_mapping[x.group()], s)

prints:

one testing two three

Leave a Comment