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...

m/Regex/mod (see The match operator)

  

/x /o

the regex is interpreted (see regex modifiers, Regex compilation, the /o modifier, qr/.../, and efficiency)

s/ Regex/Ersatztext/mod (see The substitution operator)

  

/s /m /i

the engine processes the subject text (see regex modifiers)

qr/Regex/mod (see The qr/.../ operator and regex objects)

  

/g /c /e

other behavior (see Extracting all matches: list context with /g, Matching each text element individually with /gc, The /e modifier)

split (...) (see The split operator)

  

  

Regex pragmas

  

Matching data (see Special variables controlled by matching)

use charnames ':full'; (see Names for Unicode characters)

  

$1, $2 and so on

Captured text

use overload; (see Overloading regex literals)

  

$^N $+

Highest-numbered or last-assigned $1, $2, ...

use re 'eval'; (see Interpolating code patterns)

  

@- @+

Arrays of offsets into the subject string

use re 'debug'; (see Regex debugging information)

  

$& $` $'

Matched text and text before and after it
(avoid if possible; see The variables $`, $&, and $' misbehave)

Related functions

  

Related variables

lc lcfirst uc ucfirst (see Convert the next character to lowercase or uppercase)

  

$_

Default subject text (see The default input string $_)

pos (see The current position and the pos() function) quotemeta (see Treat a range as literal text)

 

$^R

Result value (see $^R)

reset (see Special match-once-only behavior with ?...?) study (see The study function)

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

back to top