import re
sub_dict = {
'%AAA%': 'Hello',
'%BBB%': 'world'
}
pattern = '|'.join(sub_dict.keys())
repl_func = lambda matchobj: sub_dict[matchobj.group(0)]
text = "%AAA%, the %BBB% !"
text = re.sub(pattern, repl_func, text)
References :
Re: [問題] Python全文取代 - 看板 Python - 批踢踢實業坊
沒有留言:
張貼留言