All posts

Regular Expressions

Originally published on macresearch.org, around 2008. Reproduced from the author's archive; some links may no longer resolve.

Author: Drew McCormack
Web Sites: maccoremac.com, macanics.net

Regular expressions are a powerful tool for matching textual patterns. You may have seen them used in text editors like TextMate and BBEdit, and wondered what they are about. In short, they allow you to search particular patterns of text. They can be used to validate that a particular piece of textual data is correct, or to locate and extract information from a file. They can be very useful, not just in text editors, but also in scripting. Scripting languages like Python, Ruby, and Perl have very good support for regular expressions, and even command line tools like grep have limited support.

If you’ve ever wanted to learn regular expressions, here’s your chance, because I’m attaching to this post a chapter on regular expressions that I recently wrote for a Master’s level Python course. Although it targets the Python language, the regular expression syntax is virtually the same across the board, so anything you learn should translate pretty well to other languages and tools.