Reputation: 327
Consider I have the string of the following:
string = "Hello, please send message to @david, @nick, @jack, but do not send message to any email address like [email protected] or [email protected], thanks!"
matches = re.findall("\@\w+", string)
print(macthes)
#return ['@david', '@nick', '@jack', '@google', '@yahoo']
However, I would only want to return ['@david', '@nick', '@jack']
How can I exclude the pattern for email address so that it only return me the name tag using @. Thanks.
Upvotes: 1
Views: 240
Reputation: 626689
Since emails contain a word char before @
you may use \B
:
r'\B@\w+'
The \B
here matches at the start of the string or if there is a non-word char before @
(punctuation other than _
or whitespace). See the regex demo.
If you know the strings you need to extract are after whitespace/start of string use
r'(?<!\S)@\w+'
The (?<!\S)
negative lookahead fails the match if there is no whitespace immediately to the left of the current location. Hence, @\w+
only matches at the start of the string or after a whitespace. See this regex demo.
import re
s = 'Hello, please send message to @david, @nick, @jack, but do not send message to any email address like [email protected] or [email protected], thanks!'
print( re.findall(r'\B@\w+', s) )
# => ['@david', '@nick', '@jack']
print( re.findall(r'(?<!\S)@\w+', s) )
# => ['@david', '@nick', '@jack']
Upvotes: 2