XML technologies / Regular expressions
(Excerpt from "Reguläre Ausdrücke" by Jeffrey E. F. Friedl)
This book explores a powerful tool called regular expressions.
You will learn to solve problems with regexes and make the most of programs that use them. More than that, you will learn to use regular expressions masterfully.
Regexes appear in many programs, including editors, word processors, system utilities, and databases, but reach their full potential in programming languages such as Java and JScript, Visual Basic and VBScript, JavaScript and ECMAScript, C, C++, C#, elisp, Perl, Python, Tcl, Ruby, PHP, sed, and awk. In some of these languages, regexes are the very heart of the language.
There are good reasons regexes occur in so many programs and languages: they are extremely expressive. At the simplest level, a regex describes a piece of text, useful for validating input or sifting large amounts of data. At a higher level, regexes let you master and control data, making it work for you. Mastering regular expressions means mastering data.
Intended audience
This book is for anyone with access to regular expressions. If you do not yet appreciate their full power, it can open a new world. Even experienced users should gain a broader perspective. After the first edition ⟦U+0003⟧ appeared, readers often emailed me: “I thought I understood regular expressions. Then I read Regular expressions. Now I understand.”
Programmers processing text, including all web programmers, will ⟦U+0002⟧ find a wealth of details, tips, guidance, and insight here. This level of depth and detail ⟦U+0002⟧simply cannot be found elsewhere.
Regexes are a concept implemented in many ways by many programs, far more than this book describes. Understanding the general idea is a short step from mastering a particular ⟦U+0002⟧ implementation. This book focuses on the concept, and most lessons in the examples extend beyond the language used.
Typographic conventions
Detailed discussion of complex text-processing problems demands precision. One extra or missing space can make a decisive difference. This book therefore uses these conventions:
A regular expression normally appears as ˹
so˼. The small corner marks mean “this is a regular expression.” Literal text, which stands for itself rather than acting like a variable, generally appears as ›so‹. Where no confusion is possible, I sometimes omit corners or quotes. Code fragments and listings appear naturally, without these marks.In regexes and literal text, I use a special ellipsis symbol. For example, [···] denotes brackets with unspecified contents, whereas […] contains three literal dots.
Without special notation, it is difficult to ⟦U+0002⟧ determine how many spaces appear in “a b.” When spaces appear in regexes, or sometimes in literal text, I use ›●‹. Thus, ›●●●●‹ clearly contains exactly four spaces.
I also use visible symbols for tab, newline, and carriage return:● One space
TAB A tab character
NL A newline character
CR A carriage-return characterI use underlining and gray shading to highlight specific parts of regexes or literal text. For example:
Because ˹
ding˼ in the sentence ›Dies●bedingt,●dass●das●Ding●an●sich···‹ does not match the word ›Ding‹, ...Here, the underlined text shows the regex match. Another example:
To actually use
˹Subject|Date˼, enclose it in parentheses and append a colon and a space:˹(Subject|Date):●˼.Here, the underlined characters are those just added.
Contents
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