Regular Expressions in Python
Chapter 31: Regular Expressions
4 short lessons, with 3D animations, quizzes and coding tasks.
The lessons
Regular expressions: patterns that find text
A regular expression is a pattern that describes the text you're looking for, like \d+ for "one or more digits", and re.findall gives back every piece of text that fits it.
Repeats and anchors: checking a whole format
Repeat marks like {11} and ? say how many times something appears, anchors like ^ and $ pin a pattern to the start and end, and re.fullmatch checks that the whole text fits.
Groups: pulling out the parts
Round brackets in a pattern make groups, so a match can hand back its parts (m.group(1)), findall gives the groups, and re.sub can rearrange them with \1, \2, \3.
More regex tools, and the traps
re.split cuts text at every separator a pattern describes, flags like re.IGNORECASE change how matching works, ? after a repeat makes it lazy, and re.escape makes ordinary text safe to put in a pattern.