26. Regular Expressions (Pattern Matching)
26.1 That's an Expression
When my young daughter is struggling to understand the meaning of an idiomatic expression, such as, "Someone let the cat out of the bag," before I tell her what it means, I have to tell her that it's an expression , that she's not to interpret it literally. (As a consequence, she also uses "That's just an expression" to qualify her own remarks, especially when she is unsure about what she has just said.)
An expression, even in computer terminology, is not something to be interpreted literally. It is something that needs to be evaluated.
Many UNIX programs use a special "regular expression syntax" for specifying what you could think of as "wildcard searches" through files. Regular expressions describe patterns, or sequences of characters, without necessarily specifying the characters literally. You'll also hear this process referred to as "pattern matching."
In this chapter, we depart a bit from the usual "tips and tricks" style of the book to provide an extended tutorial about regular expressions in article 26.4 . We did this because regular expressions are so important to many of the tips and tricks elsewhere in the book, and we wanted to make sure that they are covered thoroughly.
This tutorial article is accompanied by a few snippets of advice (articles 26.5 and 26.7 ), and a few tools that help you see what your expressions are matching (article 26.6 ). There's also a quick reference (article 26.10 ) for those of you who just need a refresher.
For tips, tricks and tools that rely on an understanding of regular expression syntax, you have only to look at:
O'Reilly's Mastering Regular Expressions , by Jeffrey Friedl, is a gold mine of examples and specifics.
- (Dale wrote the good part, in O'Reilly & Associates' sed & awk ),