我有一个字典和字符串:
d = {'ASAP':'as soon as possible', 'AFAIK': 'as far as I know'}
s = 'I will do this ASAP, AFAIK. Regards, X'
我希望将字典的键替换为字符串中对应的值并返回。
I will do this <as soon as possible>, <as far as I know>. Regards, X.
我使用
pattern = re.compile(r'\b(' + '|'.join(d.keys())+r')\b')
result=pattern.sub(lambda x: '<'+d[x.group()]+'>',s)
print"result:%s" % result
我有一个类似于字典的数据结构:
{'will you wash some pants for me please :-)': 'text'}
笑脸表情符号导致了错误。我该如何修改正则表达式以适应任何像笑脸这样的字符?
re.escape
了吗? - Corley Brigman