Pattern-Matching Operators

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 :




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:




Evaluate the right side as an expression.


Replace globally, i.e., all occurrences.


Continue search after g failed. ...

Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.