This section is based on the documentation of the package GNU RegExp.
A regular expression consists of a character string in which some characters are given special meaning with regard to pattern matching. Regular expressions have been in use from the early days of computing, and provide a powerful and efficient way to parse, interpret, and search and replace text within an application.
Within a regular expression, the following characters have special meaning.
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 nonword 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).
Matches any single character.
Matches any decimal digit.
Matches any nondigit.
Matches a newline character.
Matches a return character.
Matches any whitespace character.
Matches any nonwhitespace character.
Matches a horizontal tab character.
Matches any word (alphanumeric) character.
Matches any nonword (alphanumeric) character.
Matches the character x, if x is not one of the above listed escape sequences.
Character Class Operator
Matches any character in the set a, b, or c.
Matches any character not in the set a, b, or c.
Matches any character in the range a to z, inclusive.
Leading or trailing dash
Within a character class expression, the following sequences have special meaning if the syntax bit RE_CHAR_CLASSES is enabled:
Any alphanumeric character.
Any alphabetic character.
A space or horizontal tab.
A control character.
A decimal digit.
A nonspace, noncontrol 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.
Subexpressions and Back References
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.
Where 0 < n < 10, matches the same thing the nth subexpression matched.
Branching (Alternation) Operator
Matches whatever the expression a or b would match.
These symbols operate on the previous atomic expression:
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.
Matches exactly m repetitions of the one-character expression.
Matches between m and n repetitions of the preceding expression, inclusive.
Matches m or more repetitions of the preceding expression.
Stingy (Minimal) Matching
If a repeating operator (above) is immediately followed by a question mark (?), the repeating operator stops at the smallest number of repetitions that can complete the rest of the match.
Lookahead refers to the ability to match part of an expression without consuming any of the input text. There are two variations to this:
Matches at any position where foo would match, but does not consume any characters of the input.
Matches at any position where foo would not match, but does not consume any characters of the input.
Some flavors of regular expression utilities support additional escape sequences. The following is not meant to be an exhaustive list. In the future, gnu.regexp might support some or all of the following: