Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Hashtag后跟常规文本_Python_Regex_Tweets - Fatal编程技术网

Python Hashtag后跟常规文本

Python Hashtag后跟常规文本,python,regex,tweets,Python,Regex,Tweets,我想检查一个hashtag后面是常规文本还是python字符串中的另一个hashtag。 例如,本案: "my adjectives names #Day #Night which are in the description" ,我得到false,因为在第一个hashtag之后又出现了一个hashtag。 但在其他情况下,例如 "my adjectives names #Day which is in the description" 我会实现的。 如何使用Python中的正则表达式操作

我想检查一个hashtag后面是常规文本还是python字符串中的另一个hashtag。 例如,本案:

"my adjectives names #Day #Night which are in the description"
,我得到false,因为在第一个hashtag之后又出现了一个hashtag。 但在其他情况下,例如

"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 '>
>>重新导入
>>>pat=re.compile(r'(#\w+\s+{2,}'))
>>>
>>>text='我的形容词名称#描述中的日期'
>>>专利检索(文本)
>>>
>>>text='我的形容词名称#Day#Night,在描述中'
>>>专利检索(文本)
对于后面没有另一个标签的标签,请使用:

input = "my adjectives names #Day #Night which are in the description"
matches = re.findall(r'#[^#\s]+\b(?!\s+#[^#]+)', input)
print(matches)

['#Night']
对于紧跟另一个hashtag的hashtag,只需将负前瞻替换为正前瞻:

matches = re.findall(r'#[^#\s]+\b(?=\s+#[^#]+)', input)
print(matches)

['#Day']

向我们展示一些代码将非常有帮助……您实际上希望匹配两种类型的hashtag中的哪一种?这正是我搜索的内容。非常感谢!:)