Sam Toorchi
Sam Toorchi

Reputation: 143

Hashtag followed after by a regular text

I want to check if a hashtag is followed by a regular text or another hashtag in a python string. for example for the case:

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

, I get false, because after the first hashtag comes again a hashtag. But in other cases for example

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

I will get true. How can I do that with regular expression operations in Python?

I tried:

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)

but it doesn't give me any outputs.

Upvotes: 1

Views: 67

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522797

For hashtags which are not followed by another hashtag use:

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

['#Night']

And for hashtags which are followed by another hashtag, just replace the negative lookahead with a positive one:

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

['#Day']

Upvotes: 0

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14975

An example from the interpreter:

>>> 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 '>

Upvotes: 1

Related Questions