Predict the Regex Output
Intermediate · Predict the output · 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 lessonWhat will this code print? Think carefully about greedy vs lazy matching and what findall returns with groups.
import re
text = '<b>bold</b> and <i>italic</i>'
greedy = re.findall(r'<(.+)>', text)
print(greedy)
lazy = re.findall(r'<(.+?)>', text)
print(lazy)
More Regex exercises
- Find a Phone NumberIntermediate
- Extract All HashtagsIntermediate
- 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