gpt4 book ai didi

python - 使用正则表达式查找包含重复单词的句子

转载 作者:行者123 更新时间:2023-12-04 00:53:05 27 4
gpt4 key购买 nike

我已经在下面尝试过了。

import re
sentences = "Glitches happened happened happened. Things go back to normal again."
print([re.findall(r"(\w+)\s\1", s) for s in sentences.split('.')])

我想知道如何打印包含重复单词的整个句子。

最佳答案

作为解决方法,您可以尝试:

import re

sentences = "Glitches happened happened happened. Things go back to normal again. And once again again again."
print([s for s in sentences.split('.') if re.search(r"\b(\w+)\s+\1\b", s)])

结果:

['Glitches happened happened happened', ' And once again again again']

关于python - 使用正则表达式查找包含重复单词的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64870516/

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