所以我有一个测试字符串
content = 'I opened my mouth, "Good morning!" I said cheerfully'
我想使用正则表达式删除双语音标记之间的文本,但不删除语音标记本身。所以它会回来
'I opened my mouth, "" I said cheerfully'
我正在使用以下代码
content = re.sub(r'".*"'," ",content)
但这也消除了双重语音标记。我应该使用哪种模式来保留语音标记,但删除其中的文本。
使用'""'
作为替换字符串:
>>> content = 'I opened my mouth, "Good morning!" I said cheerfully'
>>> content = re.sub(r'".*"', '""', content)
>>> print(content)
I opened my mouth, "" I said cheerfully
顺便说一句,.*
尽可能匹配(贪婪)。要匹配非贪婪的方式,请使用.*?
或[^"]*
。
>>> content = 'I opened my mouth, "Good morning!" I said cheerfully. "How is everyone?"'
>>> content = re.sub(r'".*?"', '""', content)
>>> print(content)
I opened my mouth, "" I said cheerfully. ""
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句