我有带有占位符的文本,例如:
sometext $plc_hldr1 some more text $plc_hldr2 some more more text $1234date_placeholder some text $5678date_placeholder
然后我有字典,其中键代表占位符,值是占位符应该被替换的值:
placeholders = {'$plc_hldr1': '1111',
'$plc_hldr2': 'abcd'}
我找到并调整了处理替换的函数:
def multiple_replace(adict, text):
# Create a regular expression from all of the dictionary keys
regex = re.compile("|".join(map(re.escape, adict.keys( ))))
# For each match, look up the corresponding value in the dictionary
return regex.sub(lambda match: adict[match.group(0)], text)
该函数正在为 $plc_hldr1
和 $plc_hldr2
工作。
但是有 $1234date_placeholder
和 $5678date_placeholder
- 两者都应替换为一个预定义值。在这种情况下,date_placeholder
保持不变,但数字部分始终不同。
我想到的是:
def multiple_replace(adict, text):
# Create a regular expression from all of the dictionary keys
regex = re.compile("|".join(map(re.escape, adict.keys( ))))
regex = re.sub("\$\d*date_placeholder", "20200101", txt)
# For each match, look up the corresponding value in the dictionary
return regex.sub(lambda match: adict[match.group(0)], text)
但是有没有更优雅的方式来做到这一点?如果我有更多带有可变数字部分的占位符,它们应该替换为相同的值(例如 $1234dname_placeholder、$1234age_placeholder)?
答案 0 :(得分:1)
如果其余的占位符不需要转义,您可以将 \$\d*date_placeholder
与其余的占位符结合使用。然后,创建第二个没有任何特殊正则表达式字符的字典,用于查找替换正则表达式匹配的内容。
map(re.escape, adict.keys())
在上面的代码中是必需的,因为占位符名称中有特殊的正则表达式字符 $
。我建议您自己添加特殊字符转义,并将您的 \$\d*date_placeholder
查找添加为 placeholders
中的键/值对。这样既不需要将 re.escape
映射到您的键上,也不需要在 multiple_replace
函数中使用第二个替换。
像这样...
import re
placeholders = {r'\$plc_hldr1': '1111',
r'\$plc_hldr2': 'abcd',
r'\$\d*date_placeholder': '20200101'}
def remove_escape_chars(reggie):
return re.sub(r'\\\$\\d\*|\$\d*|\\', '', reggie)
def multiple_replace(escape_dict, text):
# Create a second dictionary to lookup regex match replacement targets
unescaped_placeholders = { remove_escape_chars(k): placeholders[k] for k in placeholders }
# Create a regular expression from all of the dictionary keys
regex = re.compile("|".join(escape_dict.keys()))
return regex.sub(lambda match: unescaped_placeholders[remove_escape_chars(match.group(0))], text)
text = "sometext $plc_hldr1 some more text $plc_hldr2 some more more text $1234date_placeholder some text $5678date_placeholder"
result = multiple_replace(placeholders, text)
print(result)
这种方法的缺点是,如果您将新模式引入占位符,则必须更新 remove_escape_chars(...)
函数中的正则表达式。 (它将扩展到类似的模式,例如 $1234dname_placeholder
或 $1234age_placeholder
。)