Extract hashtags and mentions

EasyRegEx Programs · 7 of 20

Write a Python program to extract hashtags and @mentions from a tweet.

Advertisement
Output
['#python', '#testing']
['@sqatools', '@asha_k']

Approach

  1. #\w+ and @\w+.

Solution

Python
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.

More regex programs

← All 20 RegEx Programs

Leave a Comment