XML technologies / Regular expressions / Perl / Regular expressions as part of the language
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
The attraction of regular expressions in Perl is their closeness to the language’s core. They are not merely a few library functions, but regex-operators tightly interwoven with the other operators and constructs making up the language.
Sometimes almost everything in Perl seems connected to regexes. In fact, only four operators directly concern them, plus a few related language elements listed below.
Table: overview of regex elements in Perl.
Regex operators |
| Modifiers | Affect how... |
|---|---|---|---|
|
|
| the regex is interpreted (see regex modifiers, Regex compilation, the /o modifier, qr/.../, and efficiency) |
|
|
| the engine processes the subject text (see regex modifiers) |
|
|
| other behavior (see Extracting all matches: list context with /g, Matching each text element individually with /gc, The /e modifier) |
|
|
| |
Regex pragmas |
| Matching data (see Special variables controlled by matching) | |
|
|
| Captured text |
|
|
| Highest-numbered or last-assigned $1, $2, ... |
|
|
| Arrays of offsets into the subject string |
|
|
| Matched text and text before and after it |
Related functions |
| Related variables | |
|
|
| Default subject text (see The default input string $_) |
|
|
| Result value (see $^R) |
| |||
Perl is powerful and versatile, but that power can be a double-edged sword.
Perl’s greatest strengths
The rich options and uses of regex operators and functions are perhaps Perl’s greatest strength. They adapt to context, often exactly as the programmer intends. Programmieren mit Perl, published by O’Reilly, boldly claims: “In general, Perl operators do what you expect…” The match operator m/Regex/, for example, behaves differently depending on its usage, context, and modifiers.
Perl’s greatest weaknesses
This expressive richness is also one of Perl’s greatest weaknesses. Countless special cases, edge conditions, and contexts seem to change arbitrarily without warning. (This chapter does at least try to enumerate them.) A tiny script change can trigger a special case whose existence you never suspected. The quotation from Programmieren mit Perl continues: “…unless you expect consistency.” In computing, consistent and reliable interfaces can be small works of art, but are often boring, which Perl certainly is not. In skilled hands, Perl can be an excellent tool; mastery simply seems to require hitting your thumb instead of the nail a few times first.
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