An interesting combination of regularity and lambda (Python 3)

Time:2019-8-14

【foo.py】

# encoding: utf-8
# author: walker
# date: 2019-07-15
# Summy: An Interesting Combination of Regularity and 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)      # [email protected]

[Relevant Reading]

  • re.sub
  • lambda