在Python中使用字典进行搜索和替换

在Python中使用字典进行搜索和替换,python,python-3.x,Python,Python 3.x,我有一本字典,我正在用它做搜索和替换工作。这是我的密码: import re d = { 'non':'nonz', '$non':'nonzz', 'non profit':'non profitz' } line1 = 'non non profit' line2 = '$non non profit' ''' The regex is constructed from all the keys by sorting them in descending order of lengt

我有一本字典,我正在用它做搜索和替换工作。这是我的密码:

import re

d = {
'non':'nonz',
'$non':'nonzz',
'non profit':'non profitz'
}

line1 = 'non non profit'
line2 = '$non non profit'

'''
The regex is constructed from all the keys by sorting them in descending 
order of length and joining them with the | regex alternation operator. 
The sorting is necessary so that the longest of all possible choices is 
selected if there are any alternatives.
'''
regex = re.compile(r'\b(' + '|'.join(re.escape(key) for key in sorted(d, key=len, reverse=True)) + r')\b')

line1 = regex.sub(lambda x: d[x.group()], line1)
line2 = regex.sub(lambda x: d[x.group()], line2)

print (line1) # nonz non profitz
print (line2) # $nonz non profitz
# line1 is being replaced correctly
# However, I am expecting line2 to be: nonzz non profitz
# i.e. I am expecting $non from line2 to be replaced with nonzz

请帮助我确定问题。

您是按长度排序的,因此non优先于$non<代码>'\b'与空字符串匹配,但仅在单词的开头或结尾处匹配。单词定义为字母数字或下划线字符序列,因此单词的结尾由空格或非字母数字、非下划线字符表示。因此,
“$”
不是有效的单词字符。