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. |
|
Matches the start of the entire string. |
|
Matches the end of the entire string. |
|
Matches at a word break (Perl5 syntax only). |
|
Matches at a non-word break; opposite of |
|
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. |
|
Matches any decimal digit. |
|
Matches any non-digit. |
|
Matches a newline character. |
|
Matches a return character. |
|
Matches any whitespace character. |
|
Matches any non-whitespace character. |
|
Matches a horizontal tab character. |
|
Matches any word (alphanumeric) character. |
|
Matches any non-word (alphanumeric) character. |
|
Matches the character x , if x is not one of the above-listed escape sequences. |
Character Class Operator
Operator |
Description |
---|---|
|
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. |
|
A non-space, non-control character. |
|
A lowercase letter. |
|
Same as graph, but also space and tab. |
|
A punctuation character. |
|
Any whitespace character, including newline and return. |
|
An uppercase letter. |
|
A valid hexadecimal digit. |
Sub-expressions and Back References
Operator |
Description |
---|---|
|
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. |
|
Matches the same thing the n th 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. |