Skip to content Skip to sidebar Skip to footer

Regex To Extract Mentions In Twitter

I need to write a regex in python to extract mentions from Tweets. My attempt: regex=re.compile(r'(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9]+)') It works fine for an

Solution 1:

Add an underscore to the last set like this:

(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9_]+)

Regex101 Demo

On a side note, Twitter Handle rules allow you to have usernames starting with numbers & underscores as well. So to extract twitter handles a regex could be as simple as: @\w{1,15}(allows characters, numbers and underscores and includes the 15 character limit). Will need some additional lookaheads/lookbehinds based on where the regex might be used.

Solution 2:

A shorter version, including the negative cases from @degant:

(?<=@)\w+

Post a Comment for "Regex To Extract Mentions In Twitter"