Learn: Regular expressions¶
Four short tutorials that build up your working knowledge of the re module from the first pattern to capturing groups and substitution. Each one is a Jupyter notebook — read it in the browser, and edit and run any code cell directly on the page.
The tutorials build on each other, so working through them in order is the fastest path. Each takes roughly fifteen to twenty minutes.
The sequence¶
- Your first pattern —
re.search, literal matches, and reading what a pattern actually says. - Character classes and quantifiers —
\d,\w,[abc],*,+,?, and the difference between greedy and lazy. - Groups and capturing — parentheses, named groups, and pulling structured data out of a match.
- Find and replace —
re.sub, backreferences, and using a function as the replacement.
Before you start¶
You'll get more from these if you're already comfortable with Python strings and methods. The String processing guide is a good warm-up.
You don't need Python installed locally — every tutorial runs in your browser. If you'd rather run them on your own machine, each notebook has a download button in the top-right corner.