Support for Special Constructs “\” in Regular Expressions
In general, \ followed by any character matches only that character. There are, however, several exceptions to this rule, where characters preceded by \ are special constructs. Such characters are always ordinary when encountered on their own.
The following table describes possible \ special constructs.
\ Special Construct
|
Description of Use
|
|
\|
|
This specifies an alternative.
For example, foo\|bar matches either foo or bar but no other string.
Full backtracking capability allows you to handle multiple uses of \|.
|
|
\( ... \)
|
This grouping construct serves three purposes:
-
To enclose a set of
\| alternatives for other operations.
Therefore, \(foo\|bar\)x matches either foox or barx. -
To enclose a complicated expression for the postfix
* to operate on. Therefore, ba\(na\)* matches ‘bananana’ with any (zero or more) number of na strings -
To mark a matched substring for future reference.
See
\n below.
|
|
\n
|
After the end of a \( ... \) construct, the matcher remembers the beginning and end of the text matched by that construct. Then, later on in the expression, you can use \ followed by n to mean “match the same text matched the nth time by the
\( ...\) construct.”
|
|
\‘
|
This matches the empty string, provided it is at the beginning of the buffer.
|
|
\’
|
This matches the empty string, provided it is at the end of the buffer.
|
|
\B
|
This matches the empty string, provided it is not at the beginning or end of a word.
|
|
\<
|
This matches the empty string, provided it is at the beginning of a word.
|
|
\>
|
This matches the empty string, provided it is at the end of a word.
|
|
\w
|
This matches any word-constituent character determined by the editor syntax table.
|
|
\W
|
This matches any character that is not a word-constituent.
|
|
\scode
|
This matches any character whose syntax is code (a character that represents syntax code)
|
|
\Scode
|
This matches any character whose syntax is not code.
|
Related Topics
Support for Special Characters in Regular Expressions
Return to top