Overview of Regular Expressions

Copyright (c) 1998-2001, Dr John Maddock

For a comprehensive review of Regular Expressions see http://www.cs.utah.edu/dept/old/texinfo/regex/regex_toc.html.

Literals

All characters are literals except: ., |, *, ?, +, (, ), {, }, [, ], ^, $ and \. These characters are literals when preceded by a "\". A literal is a character that matches itself, or matches the result of traits_type::translate(), where traits_type is the traits template parameter to class reg_expression.

Wildcard

The dot character "." matches any single character except when:

Repeats

A repeat is an expression that is repeated an arbitrary number of times.

Examples

Non-greedy repeats

Whenever the "extended" regular expression syntax (the default) is in use then non-greedy repeats are possible by appending a '?' after the repeat; a non-greedy repeat is one which will match the shortest possible string.

For example to match html tag pairs one could use something like: <\s*tagname[^>]*>(.*?)<\s*/tagname\s*>

In this case $1 will contain the text between the tag pairs, and will be the shortest possible matching string.

Parenthesis

Parentheses serve two purposes, to group items together into a sub-expression, and to mark what generated the match. For example the expression (ab)* would match all of the string ababab.

Non-Marking Parenthesis

Sometimes you need to group sub-expressions with parenthesis, but do not want the parenthesis to spit out another marked sub-expression, in this case a non-marking parenthesis (?:expression) can be used. For example the following expression creates no sub-expressions: ( ?:abc) *

Forward Lookahead Asserts

There are two forms of these; one for positive forward look-ahead asserts, and one for negative look-ahead asserts:

Alternatives

Alternatives occur when the expression can match either one sub-expression or another, each alternative is separated by a "|", or a "\|" if the flag regbase::bk_vbar is set, or by a newline character if the flag regbase::newline_alt is set. Each alternative is the largest possible previous sub-expression; this is the opposite behaviour from repetition operators.

Examples

Sets

A set is a set of characters that can match any single character that is a member of the set. Sets are delimited by "[" and "]" and can contain literals, character ranges, character classes, collating elements and equivalence classes. Set declarations that start with "^" contain the compliment of the elements that follow.

Examples

Character literals:

Character ranges:

Character classes are denoted using the syntax [:classname:] within a set declaration, for example [[:space:]] is the set of all white-space characters. Character classes are only available if the flag regbase::char_classes is set. The available character classes are:

There are some shortcuts that can be used in place of the character classes, provided the flag regbase::escape_in_lists is set then you can use:

Collating elements take the general form [.tagname.] inside a set declaration, where tagname is either a single character, or a name of a collating element, for example [[.a.]] is equivalent to [a], and [[.comma.]] is equivalent to [,]. The library supports all the standard POSIX collating element names, and in addition the following digraphs: "ae", "ch", "ll", "ss", "nj", "dz", "lj", each in lower, upper and title case variations. Multi-character collating elements can result in the set matching more than one character, for example [[.ae.]] would match two characters, but note that [^[.ae.]] would only match one character.

Equivalence classes take the general form [=tagname=] inside a set declaration, where tagname is either a single character, or a name of a collating element, and matches any character that is a member of the same primary equivalence class as the collating element [.tagname.]. An equivalence class is a set of characters that collate the same, a primary equivalence class is a set of characters whose primary sort key are all the same (for example strings are typically collated by character, then by accent, and then by case; the primary sort key then relates to the character, the secondary to the accentation, and the tertiary to the case). If there is no equivalence class corresponding to tagname, then [=tagname=] is exactly the same as  [.tagname.]. Unfortunately there is no locale independent method of obtaining the primary sort key for a character, except under Win32. For other operating systems the library will "guess" the primary sort key from the full sort key (obtained from strxfrm ), so equivalence classes are probably best considered broken under any operating system other than Win32.

To include a literal "-" in a set declaration then: make it the first character after the opening "[" or "[^", the endpoint of a range, a collating element, or if the flag regbase::escape_in_lists is set then precede with an escape character as in "[\-]". To include a literal "[" or "]" or "^" in a set then make them the endpoint of a range, a collating element, or precede with an escape character if the flag regbase::escape_in_lists is set.

Line anchors

An anchor is something that matches the NULL string at the start or end of a line: "^" matches the NULL string at the start of a line, "$" matches the NULL string at the end of a line.

Back references

A back reference is a reference to a previous sub-expression that has already been matched, the reference is to what the sub-expression matched, not to the expression itself. A back reference consists of the escape character "\" followed by a digit "1" to "9", "\1" refers to the first sub-expression, "\2" to the second etc. For example the expression (.*)\1 matches any string that is repeated about its mid-point for example abcabc or xyzxyz. A back reference to a sub-expression that did not participate in any match, matches the NULL string: NB this is different to some other regular expression matchers. Back references are only available if the expression is compiled with the flag regbase::bk_refs set.

Characters by code

This is an extension to the algorithm that is not available in other libraries, it consists of the escape character followed by the digit "0" followed by the octal character code. For example "\023" represents the character whose octal code is 23. Where ambiguity could occur use parentheses to break the expression up: "\0103" represents the character whose code is 103, "(\010)3 represents the character 10 followed by "3". To match characters by their hexadecimal code, use \x followed by a string of hexadecimal digits, optionally enclosed inside {}, for example \xf0 or \x{aff}, notice the latter example is a Unicode character.

Word operators

The following operators are provided for compatibility with the GNU regular expression library.

The start of the sequence passed to the matching algorithms is considered to be a potential start of a word unless the flag match_not_bow is set. The end of the sequence passed to the matching algorithms is considered to be a potential end of a word unless the flag match_not_eow is set.

Buffer operators

The following operators are provide for compatibility with the GNU regular expression library, and Perl regular expressions:

A buffer is considered to consist of the whole sequence passed to the matching algorithms, unless the flags match_not_bob or match_not_eob are set.

Escape operator

The escape character "\" has several meanings.

Single character escape sequences

The following escape sequences are aliases for single characters:

Escape sequence

Character code

Meaning

\a 0x07 Bell character
\f 0x0C Form feed
\n 0x0A Newline character
\r 0x0D Carriage return
\t 0x09 Tab character
\v 0x0B Vertical tab
\e 0x1B ASCII Escape character
\0dd 0dd An octal character code, where dd is one or more octal digits.
\xXX 0xXX A hexadecimal character code, where XX is one or more hexadecimal digits.
\x{XX} 0xXX A hexadecimal character code, where XX is one or more hexadecimal digits, optionally a unicode character.
\cZ z-@ An ASCII escape sequence control-Z, where Z is any ASCII character greater than or equal to the character code for '@'.

Miscellaneous escape sequences:

The following are provided mostly for Perl compatibility, but note that there are some differences in the meanings of \l \L \u and \U :

What gets matched?

The regular expression library will match the first possible matching string, if more than one string starting at a given location can match then it matches the longest possible string, unless the flag match_any is set, in which case the first match encountered is returned. Use of the match_any option can reduce the time taken to find the match - but is only useful if the user is less concerned about what matched - for example it would not be suitable for search and replace operations. In cases where their are multiple possible matches all starting at the same location, and all of the same length, then the match chosen is the one with the longest first sub-expression, if that is the same for two or more matches, then the second sub-expression will be examined and so on.

See Also

REGEX_MATCH(), REGEX_MERGE(), REGEX_SPLIT(), Text, Numbers, and Punctuation in Regular Expressions