Database Reference
In-Depth Information
$val
=~
s
/
pat
/
replacement
/
;
# substitution
$val
=~
s
/
pat
/
replacement
/
i
;
# case-insensitive substitution
$val
=~
s
/
pat
/
replacement
/
g
;
# global substitution
$val
=~
s
/
pat
/
replacement
/
ig
;
# case-insensitive and global
The following table shows some of the special pattern elements available in Perl regular
expressions:
Pattern What the pattern matches
^
Beginning of string
$
End of string
.
Any character
\s
,
\S
Whitespace or nonwhitespace character
\d
,
\D
Digit or nondigit character
\w
,
\W
Word (alphanumeric or underscore) or nonword character
[...]
Any character listed between the square brackets
[^...]
Any character not listed between the square brackets
p1
|
p2
|
p3
Alternation; matches any of the patterns
p1
,
p2
, or
p3
*
Zero or more instances of preceding element
One or more instances of preceding element
+
n
instances of preceding element
{
n
}
m
through
n
instances of preceding element
{
m
,
n
}
Many of these pattern elements are the same as those available for MySQL's
REGEXP
regular-expression operator (see
Recipe 5.9
).
To match a literal instance of a character that is special within patterns, such as
*
,
^
, or
$
, precede it with a backslash. Similarly, to include a character within a character class
construction that is special in character classes (
[
,
]
, or
-
), precede it with a backslash.
To include a literal
^
in a character class, list it somewhere other than as the first character
between the brackets.
Many of the validation patterns shown in the following sections are of the form
/^
pat
$/
. Beginning and ending a pattern with
^
and
$
has the effect of requiring
pat
to match
the entire string that you test. This is common in data validation contexts because it's
generally desirable to know that a pattern matches an entire input value, not only part
of it. (To be sure that a value represents an integer, for example, it does no good to know
only that it contains an integer somewhere.) This is not a hard-and-fast rule, however,
and sometimes it's useful to perform a more relaxed test by omitting the
^
and
$
char‐
acters as appropriate. For example, if you want to strip leading and trailing whitespace
from a value, use one pattern anchored only to the beginning of the string, and another
anchored only to the end: