XML technologies / Regular expressions / Features and dialects / A tour of the world of regular expressions / A brief overview
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
The following table compares a few features of common programs, illustrating how different their dialects have become. It is only a summary, offering a superficial view of selected aspects of several regex dialects.
Books often use such tables to illustrate differences between programs, but they show only the iceberg’s tip. Every listed feature hides a dozen subtle but important issues.
Table: Summary of regex dialects in selected programs.
Feature | Modern grep | Modern egrep | Emacs | Tcl | .NET | Sun’s Java package | |
|---|---|---|---|---|---|---|---|
| ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
|
|
|
|
|
|
Grouping |
|
|
|
|
|
|
|
| ✓ | ✓ | ✓ | ||||
Word boundaries |
|
|
|
|
|
| |
| ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
Backreferences | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
(✓: supported) | |||||||
First, remember that most programs keep evolving. Tcl once lacked backreferences but now supports them. It formerly used the peculiar word-boundary constructs ˹[:<:]˼ and ˹[:>:]˼. They still work, but new programs should use ˹\m˼, ˹\M˼ and ˹\y˼ for start of word, end of word, and word boundary.
Programs such as grep and egrep do not come from one vendor, so several implementations exist. Anyone can write a program called grep implementing a personal regex dialect. Different tastes soon produce special features and quirks. GNU versions, for example, are often better and more robust.
Subtle and less subtle differences may matter as much as supported features. The table could suggest that Perl, .NET, and Java regexes are identical; they are far from it. Many questions remain:
Can star and other quantifiers apply to parenthesized expressions?
Does dot include newline? Do negated classes include it? What about NUL?
Are line anchors genuine line anchors, recognizing logical lines inside the text, or rather subject-string anchors? Both? Neither? Are they full metacharacters or allowed only at certain regex positions?
Are backslash escapes allowed in character classes? What else is allowed or forbidden?
May parentheses nest, and how deeply? How many pairs may the whole expression contain?
With case-insensitive matching and backreferences, must a backreference preserve the capture’s case? Do backreferences behave correctly in edge cases?
Are octal escapes such as
\123allowed? If so, how are conflicts with backreferences resolved? Are hexadecimal escapes available? Is it really the regex engine that handles octal and hexadecimal escapes, or another part of the tool?Does ˹
\w˼ match strictly alphanumerics or other characters too? The table’s programs interpret ˹\w˼ differently. Does ˹\w˼ agree with word-boundary metacharacters? Does the meaning of ˹\w˼ depend on locale? Does ˹\w˼ also work with Unicode?
Much lies behind even a neat little table like this. Remembering the dirty laundry behind the clean facade avoids most difficulties.
As noted earlier, most differences are syntactic, but some run deeper. Once you learn to rewrite an egrep regex such as ˹(Jul|July)˼ for GNU Emacs as ˹\(Jul\|July\)˼, it is tempting to think everything else is identical. Not always: apparently equivalent expressions may return different results. Semantic differences in how matching works are crucial but often overlooked. Here they explain why the two expressions differ: one always matches ›Jul‹ even when the subject is ›July‹. The same semantic differences explain why reversing things, with ˹(July|Jul)˼ and ˹\(July\|Jul\)˼, both programs find the same text. How regex engines work discusses these differences.
Often, knowing what a program can do with regexes matters more than knowing how it does it or which dialect it uses. Even if Perl’s regexes were weaker than egrep, Perl’s greater flexibility would still be valuable. This chapter examines individual features; later chapters also cover language-specific features.
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