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

Perl

.NET

Sun’s Java package

*, ^, $, [...]

✓

✓

✓

✓

✓

✓

✓

? + |

\? \+ \|

? + |

? + \|

? + |

? + |

? + |

? + |

Grouping

\(...\)

(...)

\(...\)

(...)

(...)

(...)

(...)

(?:...)

✓

✓

✓

Word boundaries

\<, \>

\<\>, \b\B

\m, \M, \y

\b, \B

\b, \B

\b, \B

\w , \W

✓

✓

✓

✓

✓

✓

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:

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