Perl Cheat Sheet Page 14

ADVERTISEMENT

15. Regular expressions
Each character matches itself, unless it is one of the special characters
+?.*ˆ$()[]{}|\ . The special meaning of these characters can be escaped
using a ‘ \ ’.
matches an arbitrary character, but not a newline unless the modifier /s is
.
used.
( . . . ) groups a series of pattern elements to a single element.
matches the beginning of the target. In multi-line mode (see
) also
ˆ
m
//
m
matches after every newline character.
matches the end of the line. In multi-line mode also matches before every
$
newline character.
[ . . . ] denotes a class of characters to match. [ˆ . . . ] negates the class.
( . . . | . . . | . . . ) matches one of the alternatives.
(? #
TEXT
) Comment.
REGEXP
) Like (
REGEXP
) but does not make back-references.
(?:
REGEXP
) Zero width positive look-ahead assertion.
(?=
) Zero width negative look-ahead assertion.
(?!
REGEXP
) Embedded pattern-match modifier.
can be one or
(?
MODIFIER
MODIFIER
more of
,
,
or
.
i
m
s
x
Quantified subpatterns match as many times as possible. When followed with a ‘ ? ’
they match the minimum number of times. These are the quantifiers:
matches the preceding pattern element one or more times.
+
matches zero or one times.
?
matches zero or more times.
*
} denotes the minimum
and maximum
match count. {
} means
{
N
,
M
N
M
N
exactly
N
times; {
N
,} means at least
N
times.
A ‘ \ ’ escapes any special meaning of the following character if non-alphanumeric,
but it turns most alphanumeric characters into something special:
matches alphanumeric, including ‘ _ ’, \W matches non-alphanumeric.
\w
matches whitespace, \S matches non-whitespace.
\s
matches numeric, \D matches non-numeric.
\d
matches the beginning of the string, \Z matches the end.
\A
matches word boundaries, \B matches non-boundaries.
\b
matches where the previous
m
g
search left off.
\G
//
\n , \r , \f , \t , etc. have their usual meaning.
\w , \s and \d may be used within character classes, \b denotes a backspace in
this context.
Back-references:
\1 . . . \9 refer to matched sub-expressions, grouped with () , inside the match.
\10 and up can also be used if the pattern matches that many sub-expressions.
See also $1 . . . $9 , $+ , $& , $‘ and $’ in section ‘Special variables’.
With modifier
x
, whitespace and comments can be used in the patterns for
readability purposes.
14

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education