Regular Expression Patterns

The operator qr can be used to create patterns.

qr /pattern/ [ modifiers ]

Creates a pattern.

Patterns can also be created implicitly when using search and replace functions.

Modifiers can be used as indicated, but also inside of the pattern using {?modifiers). In this case, modifiers imsx can be switched on and off.

i

searches in a case-insensitive manner.

m

multiline mode: ^ and $ will match at embedded newline characters.

o

interpolates variables only once.

p

preserves ${^PREMATCH},${^MATCH} and ${^POSTMATCH}.

s

single-line mode: . will match embedded newline characters.

x

allows for whitespace and comments.

^

in (?^ . . . ), uses system defaults d-imsx.

These modifiers are mutually exclusive:

a

forces strict ASCII matching, repeat for stricter ASCII matching.

d

semantics depend on other settings.

l

treats the pattern with locale properties.

u

treats the pattern with full Unicode semantics.

perlre, perlretut, perlrequick, perlunicode.

The elements that form the pattern are based on regular expressions, but nowadays contain many nonregular extensions.

Basically each character matches itself, unless it is one of the special characters +?.*^$()[{|\. The special meaning of these characters can be escaped using a \.

.

Matches any character, but not a newline. In single-line mode, matches newlines as well.

( . . . )

Groups a series of pattern elements to a single element. The text the group ...

Get Perl Pocket Reference, 5th 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.