XML technologies / Regular expressions / Introduction to regular expressions / Regular expressions as a programming language

(Excerpt from "Reguläre Ausdrücke" by Jeffrey E. F. Friedl)

If regexes are new to you, the previous example’s ˹^(From|Subject):˼ will be unclear, but there is no magic. Even magic has no magic behind it: a magician simply does things that seem impossible or unnatural to the uninitiated. Learning to conceal a card in a seemingly empty hand grants magical abilities through practice. A foreign language is similar: once you know the basics, it stops sounding unintelligible.

The filename analogy

Buying this book probably means you have some idea of regexes. Even without one, you almost certainly know the underlying concept.

You know that report.txt names one specific file. With even slight Unix or DOS/Windows experience, you also know that “*.txt” selects multiple files. These patterns, called file globs or wildcards, give special meaning to a few characters: star means any characters, question mark means any one character. The glob “*.txt” begins with anything, ˹*˼, and ends with the literal ˹.txt˼. It selects files whose names begin with anything and end in .txt.

Most systems offer further special characters, but globs remain limited. That is appropriate to their narrow task: conveniently identifying groups of ordinary filenames.

Arbitrary text is far more complex: prose, poetry, programs, memos, HTML, articles, tables, manuscripts like this one, word lists, and more. A specialized tool can easily handle a particular subset, such as selecting files.

Over time, a general language developed with enough power for many applications. Programs implement and use its patterns somewhat differently, but all fall under regular expressions.

The natural-language analogy

Regexes have two kinds of characters. Special characters, like the filename analogy’s *, are called metacharacters; all others are literals. Regex metacharacters go far beyond file globs. Filenames need few, but the programming language of regexes requires many expressive ones.

Think of regexes as a language: literals are words and metacharacters grammar. Grammar combines words into meaningful sentences. In the email example, I used ˹^(From|Subject):˼ to find lines ⟦U+0002⟧ beginning with From:‹ or Subject:. The metacharacters are underlined and will be explained shortly.

Like any new language, regexes initially bewilder. To a novice they look magical; without any knowledge they look like nonsense. Just as 正規表現は簡単にです! becomes understandable to a Japanese student—note: Regular expressions are easy! A humorous aside: as explained under Features and dialects, the term regular expression originated in formal algebra. Asked what this book is about, saying regular expressions usually draws puzzled looks from people unfamiliar ⟦U+0002⟧ with computers. The Japanese term 正規表現 is equally obscure to most Japanese people, but provokes more than puzzlement: regular sounds almost like a much more ⟦U+0002⟧ common medical term for reproductive ⟦U+0003⟧ organs. Imagine the associations!—the regex

s!<emphasis>([0­9]+(\.[0­9]+){3})</emphasis>!<inet>$1</inet>!

will soon be crystal clear to you.

This example comes from the book’s production and my editor. I mistakenly used the tag <emphasis> for internet addresses. IP addresses consist of digits and dots, for example 209.204.146.22. The script uses the substitution operator of Perl with the regex

˹<emphasis>([0­9]+(\.[0­9]+){3})</emphasis>˼

to replace the incorrect tags with <inet> tags while leaving other <emphasis> tags unchanged. We will examine this in detail later so you can apply the methods in your own language.

This book’s purpose

It is unlikely that you will specifically need to replace <emphasis> with <inet>, but replacing this with that is a common problem. Rather than providing ready-made answers to specific ⟦U+0002⟧ problems, this book teaches how to think in regexes, ready for any similar problem.

  

 

 

 

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