gpt4 book ai didi

python - 将句子 include some text 替换为 Python 正则表达式

转载 作者:太空宇宙 更新时间:2023-11-03 20:30:41 25 4
gpt4 key购买 nike

我想用 Python RE 删除段落中包含单词 "...Read more" 的句子。例如。转换内容

'AAA. BBB. CCC. DDD …Read more'

简单

'AAA. BBB. CCC.'

只需去掉以“…阅读更多”结尾的未完成的句子。如您所知,此过程用于清理爬虫结果。

我已经尝试过下面的代码

paragraph=re.sub('[.] .*?…Read more$','',paragraph)

我现在得到的结果只是'AAA'。我想保留内容直到最后一个未完成的句子。

最佳答案

要去掉以“…阅读更多”结尾的未完成句子,可以使用否定字符类 [^.]* 匹配非点,然后匹配 …阅读更多 位于字符串的末尾 $

Regex demo | Python demo

import re
paragraph = "AAA. BBB. CCC. DDD …Read more"
paragraph=re.sub('[^.]*…Read more$','',paragraph)
print(paragraph)

结果

AAA. BBB. CCC. 

关于python - 将句子 include some text 替换为 Python 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57534973/

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