Chapter 22. 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 \.

The multiline and single-line modes are discussed in the section Chapter 23.


Matches any character, but not a newline. In singleline mode, matches newlines as well.

( . . . )

Groups a series of pattern elements to a single element. The text the group matches is captured for later use. It is also assigned immediately to $^N to be used during the match, e.g., in a (?{ ... }).


Matches the beginning of the target. In multiline mode, also matches after every newline character.


Matches the end of the line, or before a final newline character. In multiline mode, also matches before every newline character.

[ . . . ]

Denotes a class of characters to match. [^ . . . ] negates the class.

... | ... | ...

Matches the alternatives from left to right, until one succeeds.

(?# text )


(? [ modifier ] : pattern )

Acts like (pattern) but does not capture the text it matches. modifier can be one or more of i, m, s, or x. Modifiers can be switched off by preceding the letter(s) with a minus sign, e.g., si-xm. See page 37 for the meaning of the modifiers.

(?= pattern )

Zero-width positive look-ahead assertion.

(?! pattern )

Zero-width negative look-ahead assertion.

(?<= pattern )

Zero-width positive look-behind assertion.

(?<! pattern )

Zero-width negative look-behind assertion.

(?{ code })

Executes Perl code while matching. Always ...

Get Perl Pocket Reference, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.