XML technologies / Regular expressions / regex techniques in practice

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

You now know the basics of writing regular expressions. Let us apply them and use more advanced techniques to build complex expressions. Every regex requires a delicate balance between finding wanted matches and rejecting unwanted text. We have already seen examples exploiting greedy behavior, and some possible pitfalls; this chapter provides more.

With an NFA, matching efficiency is part of that balance too, especially discussed in The art of writing regular expressions. A poorly designed regex, even a correct one, can slow matching dramatically.

This chapter mainly uses examples to illustrate general problem-solving considerations. They remain useful even when the particular problem is not yet relevant to your work.

Even if you never work with HTML, the HTML examples may help. Writing good regular expressions is more than a skill: it is a small art form. Mastery requires extended practice, not merely memorizing rules. These examples illustrate experience I have accumulated with regexes over the years.

You need experience of your own to understand and absorb what you learn. I hope working through my examples contributes to that.

The balanced regex

Good regexes require balancing different, sometimes conflicting goals. A good expression must satisfy these criteria:

  • Match exactly what you want, and nothing else.

  • Be understandable and easy to maintain.

  • With an NFA, be efficient: reach a match or failure quickly.

These requirements often depend on context. When I simply want to run “grep” quickly from the command line to get an overview, an extra output line rarely matters, so I spend little time formulating exactly the right regex. I allow some imprecision to save time and because I will inspect the output anyway. In an important script, however, time spent getting the regex exactly right is well invested. If the problem needs a complex regex, write it that way. Learning to balance these aspects is essential.

Even within a program, efficiency depends on context. A long alternation testing command-line arguments, such as ˹^-(display|geometry|cemap|...|quick24|random|raw)$˼, is unpleasant for an NFA. But since it checks arguments only once, running a hundred times slower than the fastest version does not matter. If the expression is applied to every line of a potentially large file, however, its inefficiency affects the entire program.

  

  

 

 

 

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