XML technologies / Regular expressions / Perl / Unusual techniques with regex extensions in Perl
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Many regex features now available in several languages originated in Perl: noncapturing parentheses, lookahead and later lookbehind, free-spacing mode, indeed most modes, including ˹\A˼, ˹\z˼ and ˹\Z˼, atomic groups, ˹\G˼, and conditional (?if then|else) constructs. These are no longer Perl-specific, so the general chapters of this book covered them.
Perl’s developers remain innovative and experimental, however, so several important new extensions exist only in Perl. One particularly interesting feature lets a regex contain Perl code executed during a match attempt. Regular expressions are already tightly integrated into Perl, but this takes integration to another level. I first introduce these Perl-specific extensions briefly, then discuss them in detail.
The dynamic regex construct ˹(??{Perl-Code})˼
Whenever matching reaches this construct, its Perl code executes. The result, either a regex object or a string interpreted as a regex, is inserted into the ongoing match at that point.
In the simple example
˹^(\d+)(??{"X{$1}"})$˼the dynamic regex construct is underlined. The entire regex matches a number at the start of a line followed by exactly that many›X‹. It matches›3XXX‹or›12XXXXXXXXXXXX‹, but not›3X‹or›7XXXX‹.Looking more closely at the
›3XXX‹example, the leading˹(\d+)˼first matches›3XXX‹, setting the variable$1to›3‹. The engine then reaches the dynamic construct, executes the Perl code"X{$1}"and obtains›X{3}‹. This is interpreted as˹X{3}˼and applied within the current regex, matching›3XXX‹. After the dynamic construct,˹$˼matches at›3XXX▵‹, completing the match.Later examples show that dynamic regexes are particularly useful for recognizing arbitrarily deep nesting.
The Perl code-pattern construct ˹(?{Perl-Code})˼
Like dynamic regexes, this construct executes Perl code when the regex engine reaches the parentheses. This is more general: the code need not return any particular type. Usually, its return value is ignored; otherwise, it is available in the variable
$^R(see $^R).One case uses the return value directly: a Perl code pattern in the
ifpart of an˹(? if then|else)˼construct (see Conditional regular expressions). The result is treated as a Boolean value, selecting either thethenorelsepart.Code patterns serve many purposes and are especially useful for debugging. The following example prints a message whenever the transmission tries the regex at a new position. The code pattern appears on line 2:
"Nur ein kleines Beispiel" =~ m{
(?{ print "Suche beginnt.\n" })
\b(?: der | die | das | eine | ein )\b
}x;The regex matches only once, but prints five messages. The transmission therefore tried four positions unsuccessfully before matching at the fifth.
Overloading regex literals
With overloading, regex literals can be preprocessed however you like before reaching the engine. This can add new metacharacters and features to Perl’s dialect. For example, Perl has no dedicated start-of-word and end-of-word metacharacters, only a word-boundary metacharacter, but
\<and\>would sometimes be useful, as in other dialects. Overloading can transform these sequences behind the scenes into expressions Perl already understands.The current implementation has restrictions that severely limit overloaded regex literals. We discuss those restrictions and examples using
\<and\>later in this section.
When using code constructs, either dynamic regexes or code patterns, start with global variables until you understand their complicated interaction with my variables (see Caution with my variables in code patterns).
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