Extract All Hashtags
Intermediate · Write the code · from Python Regular Expressions: Pattern Matching from Basics to Advanced
From the lesson: Python Regular Expressions: Pattern Matching from Basics to Advanced
Imagine you're working at a help desk and need to find every email address buried in thousands of support tickets. You could scan each line character by character, but that would take forever. Regular expressions let you describe the pattern of an email address, and Python finds every match for you.
Read the lessonWrite a function extract_hashtags(text) that returns a list of all hashtags in the text. A hashtag starts with # followed by one or more word characters (letters, digits, or underscores). Return just the tag names without the # symbol.
import re
def extract_hashtags(text):
# Use re.findall() with a group to capture the tag name
pass
result = extract_hashtags('Love #Python and #coding! #100DaysOfCode')
print(result)
More Regex exercises
- Find a Phone NumberIntermediate
- Predict the Regex OutputIntermediate
- Censor Credit Card NumbersIntermediate
- Fix the Email ValidatorIntermediate
- Parse a Log EntryIntermediate
- Clean and Normalize WhitespaceAdvanced
All Python exercises · 50 practice problems · Standard Library Essentials quiz