Reputation: 235
I am trying to use regex to detect twitter mentions. The example message is, "@This_matches hello there, @doesnt@match how is it going @matches"
What i am using:
m_list = re.findall(r'(?<!\w)(@\w{1,15})(?=\s)' ,a)
The only issue i am having is that when the username is at the end of the string it will not pick it up. For example: "@testing blah" works, "blah @testing2" does not work. So when there is no character at the end of the string it will not match. How do i fix this?
Upvotes: 1
Views: 209
Reputation: 174874
You may replace the last positive lookahead with negative lookahead.
m_list = re.findall(r'(?<!\w)(@\w{1,15})(?!\S)' ,a)
Upvotes: 0
Reputation: 67988
m_list = re.findall(r'(?<!\w)(@\w{1,15})(?=\s|$)' ,a)
^^
Add $ assert position at end of a line
or simply
m_list = re.findall(r'(?<!\w)(@\w{1,15})\b' ,a)
^^
\b word boundary
Upvotes: 1