Python Practice › RegEx Programs › Program 7
Extract hashtags and mentions
Write a Python program to extract hashtags and @mentions from a tweet.
Output
['#python', '#testing'] ['@sqatools', '@asha_k']
Approach
#\w+and@\w+.
Solution
import re tweet = "Loving #python and #testing with @sqatools and @asha_k!"print(re.findall(r"#\w+", tweet))print(re.findall(r"@\w+", tweet))Output
['#python', '#testing'] ['@sqatools', '@asha_k']
Learn the concept
New to this topic? Read the lesson, then come back.