Readable Regular Expressions

By Trey Hunner

Elevator Pitch

What are regular expressions, what are they useful for, and why are they so hard to read?

We’ll learn what regular expressions are good for, how to make our own regular expressions, and how to make our regular expressions friendly and readable (yes it’s possible, sort of).

Description

In this tutorial we will break down the regular expression syntax to better understand how regular expressions work. We will learn how to dissect regular expressions, how to use regular expressions in Python, and how to make your regular expressions more readable.

We will learn how to use regular expressions for data validation, data parsing, and data normalization. We will also learn when not to use regular expressions.

This tutorial is aimed at beginner to intermediate Python programmers who are interested in learning how to use regular expressions to manipulate text data.

We’ll cover:

  • regular expression syntax
  • using regular expressions in Python
  • making regular expressions more readable
  • using regular expressions for matching and substitution

Notes

Outline:

  • Basic Syntax (30 minutes)
    • 5 minutes searching
    • 8 minutes character classes
    • 3 minutes anchors
    • 5 minutes metacharacters
    • 7 minutes quantifiers
    • 2 minutes Q&A
  • Common Idioms (30 minutes)
    • 5 minutes exercises
    • 9 minutes special sequences
    • 3 minutes word boundaries
    • 5 minutes repeats
    • 2 minutes case sensitivity
    • 6 minutes Q&A
  • Groups (30 minutes)
    • 10 minutes verbose flag
    • 2 minutes searching
    • 3 minutes grouping
    • 3 minutes capture groups
    • 2 minutes multi-search
    • 3 minutes uncapturing groups
    • 7 minutes exercises
  • More re Module (30 minutes)
    • 5 minutes alternations
    • 2 minutes split
    • 3 minutes compile
    • 40 minutes greediness
    • 5 minutes more re exercises
  • Substitutions (30 minutes)
    • 10 minutes substitutions
    • 10 minutes captures
    • 5 minutes substitution functions
    • 5 minutes substitution exercises
  • Advanced Features (30 minutes)
    • 10 minutes lookahead
    • 15 minutes advanced exercises
    • 5 minutes Q&A

I have held a similar tutorial on the topic of regular expressions in Python at PyCon 2016, OS Bridge 2016, PyTennessee 2017, and PyCon 2017 (upcoming).