Regular Expression Syntax

The simplest kind of regular expression is a literal string. More complicated patterns involve the use of metacharacters to describe all the different choices and variations that you want to build into a pattern. Metacharacters don’t match themselves, but describe something else. The metacharacters are:

Metacharacter

Meaning

\

Escapes the character(s) immediately following it

.

Matches any single character except a newline (unless /s is used)

^

Matches at the beginning of the string (or line, if /m is used)

$

Matches at the end of the string (or line, if /m is used)

*

Matches the preceding element 0 or more times

+

Matches the preceding element 1 or more times

?

Matches the preceding element 0 or 1 times

{...}

Specifies a range of occurrences for the element preceding it

[...]

Matches any one of the class of characters contained within the brackets

(...)

Groups regular expressions

|

Matches either the expression preceding or following it

The . (single dot) is a wildcard character. When used in a regular expression, it can match any single character. The exception is the newline character (\n), except when you use the /s modifier on the pattern match operator. This modifier treats the string to be matched against as a single “long” string with embedded newlines.

The ^ and $ metacharacters are used as anchors in a regular expression. The ^ matches the beginning of a line. This character should appear only at the beginning of an expression to match ...

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.