擅长:python、mysql、java
<p><strike>用这样的附加参数编译正则表达式</p>
<p><罢工>重新编译(“这里是正则表达式”,忽略案例)
</strike></p>
<p><strong>编辑1:</strong></p>
<p>好吧,结果是,由于双引号和单引号的用法不一致,您的replace\u dict的格式不正确。
以下是工作代码和预期输出:</p>
<h2>鲍勃_芭蕾舞者你知道吗</h2>
<pre><code>import re
def word_replace(text, replace_dict):
rc = re.compile(r"[A-Za-z_]\w*")
def translate(match):
word = match.group(0).lower()
print(word)
return replace_dict.get(word, word)
return rc.sub(translate, text)
old_text = """Bob: say why don't you play ball
jeff: i have no idea
bob: well maybe you should """
replace_dict = {
"bob" : "bob baller", # Everything is double quoted
"debug" : "fix",
"ship": "boat"
}
output = word_replace(old_text, replace_dict)
print(output)
$ python bob_baller.py
bob baller: say why don't you play ball
jeff: i have no idea
bob baller: well maybe you should
</code></pre>