A regular expression consists of a character string where some characters are given special meaning with regard to pattern matching. Regular expressions provide a powerful and efficient way to parse, interpret and search and replace text within an application.

The following meta-characters can be used in regular expressions for conditional connectors:

Positional Operators

Operator

Description

^

Matches at the beginning of a line.

$

Matches at the end of a line.

\A

Matches the start of the entire string.

\Z

Matches the end of the entire string.

\b

Matches at a word break (Perl5 syntax only).

\B

Matches at a non-word break; opposite of \b (Perl5 syntax only).

\<

Matches at the start of a word (egrep syntax only).

\>

Matches at the end of a word (egrep syntax only).

One-character Operators

Operator

Description

.

Matches any single character.

\d

Matches any decimal digit.

\D

Matches any non-digit.

\n

Matches a newline character.

\r

Matches a return character.

\s

Matches any whitespace character.

\S

Matches any non-whitespace character.

\t

Matches a horizontal tab character.

\w

Matches any word (alphanumeric) character.

\W

Matches any non-word (alphanumeric) character.

\x

Matches the character x, if x is not one of the above-listed escape sequences.

Character Class Operator

Operator

Description

[abc] 

Matches any character in the set a, b or c.
[^abc]  Matches any character not in the set a, b or c.
[a-z]  Matches any character in the range a to z, inclusive.
-

A leading or trailing dash will be interpreted literally.

Within a character class expression, the following sequences have special meaning if the syntax bit RE_CHAR_CLASSES is on:

Operator

Description

[:alnum:]  Any alphanumeric character.
[:alpha:]  Any alphabetical character.
[:blank:]  A space or horizontal tab.
[:cntrl:] A control character.
[:digit:] A decimal digit.

[:graph:]

A non-space, non-control character.

[:lower:]

A lowercase letter.

[:print:]

Same as graph, but also space and tab.

[:punct:]

A punctuation character.

[:space:]

Any whitespace character, including newline and return.

[:upper:]

An uppercase letter.

[:xdigit:]

A valid hexadecimal digit.

Sub-expressions and Back References

Operator

Description

(abc) 

Matches whatever the expression abc would match, and saves it as a subexpression. Also used for grouping.
(?:...) Pure grouping operator; does not save contents.
(?#...)  Embedded comment; ignored by engine.

\n

Matches the same thing the nth subexpression matched (where 0 < n < 10).

Branching (Alternation) Operator

Operator

Description

a|b  Matches whatever the expression a would match, or whatever the expression b would match.

Repeating Operators

The following symbols operate on the previous atomic expression:

Operator

Description

? Matches the preceding expression or the null string.
* Matches the null string or any number of repetitions of the preceding expression.
+ Matches one or more repetitions of the preceding expression.
{m} Matches exactly m repetitions of the one-character expression.
{m,n} Matches between m and n repetitions of the preceding expression, inclusive.
{m,} Matches m or more repetitions of the preceding expression.

Stingy (Minimal) Matching

If a repeating operator (above) is immediately followed by a ?, the repeating operator stops at the smallest number of repetitions that can complete the rest of the match.

Lookahead

Lookahead refers to the ability to match part of an expression without consuming any of the input text. There are two variations to this:

Operator

Description

 (?=foo) Matches at any position where foo would match, but does not consume any characters of the input.
 (?!foo) Matches at any position where foo would not match, but does not consume any characters of the input.