Contents

  Icon

Basic metacharacters

Anchors

Basic metacharacters

Character classes

Comments

Control and code

Grouping

Look around

Mode modifiers

Quantifiers

Specific characters

Most regular expression engines, and Perl is no different, recognise twelve basic metacharacters. (Most of these are also listed elsewhere.)


 

Item

 

Meaning:

 
 

.

 

Matches any character except a new line.
If used in “dot matches all mode” also matches a new line.

 
 

^

 

Matches start of a string.
If used in multiline mode also matches beginning of each line, (i.e. after a newline).

 
 

$

 

Matches end of a string.
If used in multiline mode also matches end of each line, (i.e. before a newline).

 

?

Match the previous character or group 1 time, if possible.

 

+

 

Match the previous character or group as many times as possible, but at least 1 time.

 
 

*

 

Match the previous character or group as many times as possible.

 
 

{Number}
{Min,}
{Min,Max}

 

Match a number of times.
Number can be either a single number, a minimum, or a minimum and a maximum. (See quantifiers.)

 
 

(…)

 

Capturing parentheses.

 
 

[…]

 

Character class.
Matches any one character between the square brackets.

 
 

…|…

 

Alternation.
Matches either the expression on the left or the right side.

 
 

\…

 

Quote, i.e. normalise, the next metacharacter.

 
 

In the table “…” stands for any one or more characters.