ono*_*ono 5 python string replace
我想替换字符串句子中的单词,例如:
What $noun$ is $verb$?
Run Code Online (Sandbox Code Playgroud)
使用实际名词/动词替换"$ $"(包括)中的字符的正则表达式是什么?
Zty*_*tyx 12
你不需要正则表达式.我会做
str = "What $noun$ is $verb$?"
print str.replace("$noun$", "the heck")
Run Code Online (Sandbox Code Playgroud)
仅在需要时使用正则表达式.它通常较慢.
鉴于您可以$noun$根据自己的喜好自由修改等,现在这样做的最佳实践可能是format在字符串上使用该函数:
"What {noun} is {verb}?".format(noun="XXX", verb="YYY")
Run Code Online (Sandbox Code Playgroud)