ai didi

python - 在带有单词 "said"的句子周围添加引号

转载 作者:太空宇宙 更新时间:2023-11-04 03:53:49 24 4
gpt4 key购买 nike

好的正则表达式大师,我有一个很长的文本,我试图在包含单词“he said”和类似变体的句子中添加引号。

例如:

s = 'This should have no quotes. This one should he said. But this one should not. Neither should this. But this one should she said.'

应该导致:

This should have no quotes. "This one should," he said. But this one should not. Neither should this. "But this one should," she said.

到目前为止,我可以很接近,但不太正确:

>>> import re
>>> m = re.sub(r'\.\W(.*?) (he|she|it) said.', r'. "\1," \2 said.', s)

结果:

>>> print m
This should have no quotes. "This one should," he said. But this one should not. "Neither should this. But this one should," she said.

如您所见,它在第一个实例周围正确地放置了引号,但对第二个实例放置得太早了。任何帮助表示赞赏!

最佳答案

评论中指出了一些不同的有效情况,但为了解决您面临的问题:

它引用了整个句子,因为它看到 one should not. 末尾的句点.您真正想要的是只引用最后 时期。因此,在匹配的括号中确保不要包含句点,如下所示:

m = re.sub(r'\.\W([^\.]*?) (he|she|it) said.', r'. "\1," \2 said.', s)

对于像 "Dr. Seuss likes to eat, she said" 这样的句子中带有句点的东西,这将失败但这是另一个问题。

关于python - 在带有单词 "said"的句子周围添加引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19825866/

24 4 0
文章推荐: python - 在 python 3 中读取和写入文件
文章推荐: linux - 如何将 Graylog2 升级到更新版本?
文章推荐: linux - FastCGI with perl - 在共享 Linux 虚拟主机上
文章推荐: Python numpy 参数排序
太空宇宙
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com