gpt4 book ai didi

Python 正则表达式替换引号中的文本,引号本身除外

转载 作者:太空狗 更新时间:2023-10-30 01:42:49 25 4
gpt4 key购买 nike

例如,我有一个测试字符串

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

BTW,.* 尽可能匹配(贪心)。要匹配非贪婪时尚,请使用 .*?[^"]*

>>> 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. ""

关于Python 正则表达式替换引号中的文本,引号本身除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22586767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com