XML technologies / Regular expressions / Perl / The match operator / The regex operand
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
The regex operand may be a regex literal or a regex object (actually also a string or any kind of expression, though that is hardly ever useful). A regex literal can also have modifiers.
Matching with a regex literal
The match operator is most often used with a regex literal written with m/.../ or /.../. The initial ›m‹ is optional when the delimiters are slashes or question marks (question marks have a special meaning, discussed later). For consistency, I always use m, even when unnecessary. As described earlier, the delimiters following m can be chosen quite freely (see Delimiters for the regex operand).
A regex literal can be followed by the general modifiers from regex modifiers. The match operator also has the two modifiers /g and /c, discussed below.
Matching with a regex object
The regex operand may also be a regex object created with qr/.../. For example:
my $regex = qr/regex/;
.
.
.
if ($text =~ $regex) {
.
.
.You can also use the notation m/.../ with a regex object. If the regex literal between the delimiters consists of exactly and only an interpolated regex object, this is exactly equivalent to the regex object alone. Thus the example above can also be written as follows:
if ($text =~ m/$regex/) {
.
.
.This follows the familiar form and also allows the /g modifier to be specified for a regex object. (Other modifiers can also be specified, but are ignored: only the modifiers “stuck” to the object take effect; see Match modifiers stick).
Default for an empty regex operand
If no regex is specified, as in m// (or in m/$regex/ when the variable $regex is empty or undefined), the last successfully used regex in the same dynamic scope is used. Earlier versions of Perl could exploit this for efficiency, but regex objects have made this approach obsolete (see Building and using regex objects).
Special match-once-only behavior with ?...?
With question marks as delimiters, the match operator behaves very unusually. Once an expression using m?...? has matched, a new search returns false until reset is called in the same package. The Perl version 1 documentation described this as: “A useful optimization when you are interested only in the first occurrence of a pattern in a series of files.” I have never seen this form in modern Perl programs.
When question marks are chosen as delimiters, the leading ›m‹ is optional, just as with slashes: ?...? is the same as m?...?.
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