# 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