Editor

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 lesson

What 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

All Python exercises · 50 practice problems · Standard Library Essentials quiz