When I mention regular expressions (often called by the terrifying nickname “grep”) to most people, their eyes roll up into the back of their heads. I guess I understand why — after all, the prospect of doing search-and-replace functions doesn’t usually make people get up and dance. And when you’re confronted with a string like
, it’s easy to shrug it off as something so bizarre that you’ll never be able to learn it.
But it’s a little sad, because if you work with text files, HTML, or even long Word documents, you will
exponentially more time by using regular expressions than you’ll need to spend to learn them.
Put simply, regular expressions let you search (and replace) based on patterns, rather than exact characters. For example, imagine searching for every phone number in a document formatted as
and replacing it with
. To make those changes by hand in a long document would take a
time; but it takes less than 30 seconds to formulate and execute a regular expression to do the same thing.
If you use BBEdit or Dreamweaver, you’ve got regular expression power ready to go. A few years ago, I wrote an article for
how using grep can really help HTML coders; we just re-posted it for all to see. Also, BBEdit provides a useful primer on regular expressions as a part of the BBEdit online help.
If you use Word, check out the March 2004 issue of
. In our Working Mac section, David Blatner provides a really straightforward introduction to the pattern-matching power that I’d wager 99% of Word users don’t have any idea exists. Sure, you might search for
(a paragraph break and a tab) and replace them with
(two paragraph breaks), but Word’s regular-expresson feature goes way beyond that. (Unfortunately, it doesn’t use the normal grep syntax, but if you’re a Word user you should learn the quirks of Microsoft’s approach — it will pay off, I guarantee you.)
Finally, geeky though it may be, if you really want to learn how to build regular expressions, check out
Mastering Regular Expressions, 2nd Edition
by Jeffrey E.F. Friedl (O’Reilly, 2002).
There, end of lecture. But the next time you find yourself changing dozens of text items by hand, you might want to think about what I’ve said.