The following list defines Perl’s pattern-matching operators. Some of the operators have alternative “quoting” schemes and have a set of modifiers that can be placed directly after the operators to affect the match operation in some way.
m/
pattern
/gimosxe
Searches a string for a pattern match. Modifiers are :
Modifier | Meaning |
---|---|
| Match globally, i.e., find all occurrences. |
| Do case-insensitive pattern matching. |
| Treat string as multiple lines. |
| Compile pattern only once. |
| Treat string as single line. |
| Use extended regular expressions. |
If /
is the
delimiter, then the initial m
is optional. With m
, you can use any pair of
non-alphanumeric, non-whitespace characters as
delimiters.
?
pattern
?
This operator is just like the m/
pattern
/
search, except it matches only
once.
qr/
pattern
/imosx
Creates a precompiled regular expression from
pattern
, which can be passed around
in variables and interpolated into other regular expressions.
The modifiers are the same as those for m//
above.
s/
pattern
/
replacement
/egimosx
Searches a string for
pattern
and replaces any match with
the replacement
text. Returns the
number of substitutions made, which can be more than one with
the /g
modifier. Otherwise,
it returns false (0
). If no
string is specified via the =~
or !~
operator, the $_
variable is searched and
modified. Modifiers are:
Modifier | Meaning |
---|---|
| Evaluate the right side as an expression. |
| Replace globally, i.e., all occurrences. |
| Continue search after |
| Do case-insensitive pattern matching. |
| Treat string as multiple lines. |
| Compile pattern only once. |
| Treat string as single line. |
| Use extended regular expressions. |
Any non-alphanumeric, non-whitespace delimiter may
replace the slashes. If single quotes are used, no
interpretation is done on the replacement string (the /e
modifier overrides this,
however).
tr/
pattern1
/
pattern2
/cds
y/
pattern1
/
pattern2
/cds
This operator scans a string character by
character and replaces all occurrences of the characters found
in pattern1
with the corresponding
character in pattern2
. It returns
the number of characters replaced or deleted. If no string is
specified via the =~
or
!~
operator, the $_
string is translated. Modifiers
are:
18.118.149.19