Regular Expression Meta Characters

The Perl regular expression syntax includes a large number of special characters--meta characters--that make it possible to possible permutations of a pattern or the context in which a pattern may occur. The following list is not complete but only lists the most common ones.

Escape Sequences

As with variable interpolation in double-quoted strings, characters with special meanings also must be escaped in regular expressions if they are to be interpreted literally. Escaped characters are preceded by a backslash, \, character.

For example, a regular expression that matches a period in a string would look as follows:

$line =~ m/\./;

Wild-card Character

The period, ., has a special meaning in regular expressions. It matches ANY one character except a newline. For example, the following expression would match any string of 3 characters beginning with i and ending with d, like 'wild', 'period', or 'index':

$line =~ m/i.d/;

Alternation

An upright slash, |, matches the regex before or after the slash. For example, the following expression will match either 'green' or 'blue':

$line =~ m/green|blue/;

Multiple alternation is possible, too. The following expression will match either 'yellow', 'green', or 'blue':

$line =~ m/yellow|green|blue/;

Quantifiers

Quantifiers specify repetitions of the character or expression that precedes it:

* occurs 0 or more times
+ occurs 1 or more times
? occurs 0 or 1 time
{n} occurs exactly n times
{n,} occurs at least n times
{n,m} occurs at least n times, but no more than m times

Anchors

Anchors don't match characters but places within a string:

^ matches at the beginning of a string or line
$ matches at the end of a string or line
\b matches at word boundary (between \w and \W)
\B matches except at word boundary