XML technologies / Regular expressions / Introduction to regular expressions / Solving practical problems
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Knowing regular expressions opens solutions you may never have imagined. I use them several times every day for small and large problems; often, small problems would become large ones without regexes.
Their benefit is obvious when they solve a large, important problem. Less obvious is their use for small, uninteresting obstacles: problems whose solutions make poor anecdotes but must be solved before work can continue. Saving an hour of mindless, repetitive work is exciting enough for me to count as interesting.
A simple example: across roughly 70 source files for this book, I needed to verify that the string ›SetSize‹ occurred as often or as rarely as ›ResetSize‹. Case did not matter, so ›setSIZE‹ counted just like ›SetSize‹. Manually scanning 32,000 lines was out of the question. Even an editor’s ordinary word-search feature was too cumbersome across that many files.
Regexes came to the rescue! A single short command line searched every file and confirmed the result. Perhaps 15 seconds to type it and 2 seconds of computation. Ha! For the solution, see The examples.
Another example: I once helped a friend with email on a machine accessible only through a slow modem. He wanted a contents list of all messages, and there were many. Even with fewer messages, assembling the headers would be tedious and monotonous. It would also expose message contents that were none of my business.
Again, regexes rescued us. A simple command using the search tool egrep; this extracted the From: and Subject: lines from each message. To tell egrep which lines to show or omit, I used the regex ˹^(From|Subject):˼.
After receiving the list, my friend requested one particular message, 5,000 lines long. Extracting it with an editor or email program would again be cumbersome. Another tool, this time sed, and another regex extracted exactly that message from the large file, making it quick and easy to send.
Saving time and effort was not particularly interesting, but better than wasting an hour in an editor. Without regex knowledge, I would never have imagined an alternative. This shows how regexes enable tasks that otherwise might not even occur to you.
Once you learn regexes, you will appreciate their value and wonder how you managed without them. (If you own a hard-disk video recorder, you know the feeling!)
Regex mastery is an extraordinarily valuable skill. This book provides the knowledge to acquire it and, I hope, the motivation as well.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln
back to top