【foo.py】
# encoding: utf-8 # author: walker # date: 2019-07-15 # summary: 一种正则和 lambda 的有趣结合 import re dic = {'user': 'walker', 'domain': '163.com'} rule = r'%user%@%domain%' email = re.sub('%[^%]*%', lambda matchobj: dic[matchobj.group(0).strip('%')], rule) print('email: %s' % email) # walker@163.com
*** walker ***