XML technologies / Regular expressions / Features and dialects
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Using two quite different tools, Perl and egrep, we have developed a feel for regular expressions. You might expect this experience to apply everywhere. Yet the comparison of egrep versions in Introduction to regular expressions and especially Perl versus Java in Extended introductory examples showed striking differences in how other languages use regexes.
When examining a regex implementation in a language or tool, three areas matter most:
Which metacharacters are supported, and what do they mean? I often call this the regex-dialect.
What is the language interface? How are regexes specified, which functions invoke them, and how is the subject text supplied?
How does the regex engine work? How does it apply a regex to the subject text? The engine type chosen by the language or tool’s author substantially affects the results.
Regular expressions and cars
These considerations resemble buying a car. Regex metacharacters are like the immediately visible features: shape, bodywork, color, leather seats, and audio system. Advertisements and brochures highlight them. Their equivalent is the metacharacter table under Extending the foundations. It provides important information, but only part of the picture.
Equally important is the interface linking regexes to the language. Some aspects are cosmetic, such as the syntax used to specify a regex. Others determine which operations exist and how easily they can be used. For a car, these are everyday human-machine interactions. Some are minor: which side has the fuel cap, or whether windows are powered. Others matter more: manual or automatic transmission? Functional questions also arise: will it fit in the garage, carry a cabinet, skis, or five adults? How easily can those adults get in and out—more easily in a minivan than a two-door car? Brochures usually mention these matters too, though in smaller print.
The last area concerns the engine and drivetrain. Here the analogy breaks down: most drivers know enough to operate their car sensibly, avoiding diesel in a gasoline engine and using the clutch with manual transmission. Regex documentation, however, rarely hints at the engine’s internal structure. Those details matter so much that the entire next chapter is devoted to them.
In this chapter
As its title suggests, this chapter surveys regex dialects and features. It covers common metacharacters and several language interfaces, the first two areas above. The third—internal engine behavior and its practical implications—is covered in the following chapters.
This is not an exhaustive account of any implementation’s features or of every tool and language interface. Rather, it surveys regular expressions and the tools that offer them. A cave dweller using only one tool need not care about other tools or variants while remaining in that cave. Since we do not, knowing something about our tools’ origins and internal workings is useful.
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