Search code examples
pythonregextweets

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.


Solution

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