13. Regular Expressions

A regular expression is a compact notation for representing a collection of strings. What makes regular expressions so powerful is that a single regular expression can represent an unlimited number of strings—providing they meet the regular expression’s requirements. Regular expressions (which we will mostly call “regexes” from now on) are defined using a mini-language that is completely different from Python—but Python includes the re module through which we can seamlessly create and use regexes.*

* A good book on regular expressions is Mastering Regular Expressions by Jeffrey E. F. Friedl, ISBN 0596528124. It does not explicitly cover Python, ...

Get Programming in Python 3: A Complete Introduction to the Python Language, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.