gpt4 book ai didi

python - 主题标签后跟常规文本

转载 作者:行者123 更新时间:2023-12-01 07:39:59 26 4
gpt4 key购买 nike

我想检查一个主题标签后面是否跟有常规文本或 python 字符串中的另一个主题标签。例如对于以下情况:

"my adjectives names #Day #Night which are in the description"

,我得到了错误,因为在第一个主题标签之后又出现了一个主题标签。但在其他情况下例如

"my adjectives names #Day which is in the description" 

我会说实话。如何在 Python 中使用正则表达式操作来做到这一点?

我尝试过:

tweet_text = "my adjectives names #Day #Night which are in the description"
pattern = re.findall(r'\B#\w*[a-zA-Z0-9]+\B#\w*[a-zA-Z0-9]*', tweet_text)
print(pattern)

但它没有给我任何输出。

最佳答案

解释器的示例:

>>> import re
>>> pat = re.compile(r'(#\w+\s+){2,}')
>>>
>>> text = 'my adjectives names #Day which are in the description'
>>> pat.search(text)
>>>
>>> text = 'my adjectives names #Day #Night which are in the description'
>>> pat.search(text)
<_sre.SRE_Match object; span=(20, 32), match='#Day #Night '>

关于python - 主题标签后跟常规文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56771490/

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