The most powerful element of regular expression syntax is the backreference, where results of a subpattern are loaded into a buffer for reuse later in the expression. Parentheses identify backreference patterns, and the buffers are numbered as each begin parenthesis is encountered from left to right in the expression. Buffer numbers begin at 1 and continue up to a maximum of n subexpressions allowed by the .NET Framework:
-
If you search [abc]([def]) in be, the first backreference match will be e.
-
If you search ([abc])([def]) in be, the first backreference match will be b and the second backreference match will be e.
-
If you search (ab(cd))ef in abcdef, the first backreference match will be abcd and the second backreference match will be cd.
-
If you search (a)+b* in aaaabbb, the first backreference match will be a.
-
If you search (a+)b* in aaaabbb, the first backreference match will be aaaa.
-
If you search ([abc])+ in aaabbbc, the first backreference match will be c.
You can access each buffer by using the form
\n, where n is one- or two-decimal digits identifying a specific buffer: \1
identifies the first buffer. For example, the regular expression (\d )\1 could
match 44, 55, or 99, but wouldn't match 24 or 83.
One of the simplest, most useful applications of backreferences is to locate the
occurrence of two identical words together-for example, in Were you drunk or
sober last night night night? The expression \b([a-z]+) \1\b will match night
night.
To be complete, a backreference expression must be enclosed in parentheses. The
expression (\w(\1)) contains an invalid backreference since the first set of
parentheses is not complete where the backreference appears.
Here is a more advanced example where we validate a URI (universal resource
identifier), such as http://www.mindcracker.com:8080/myfolder/index.html#content1.
The regular expression (\w+):\/\/([^/:]+)(:\d*)?([^# ]*) does the following:
-
(\w+):\/\/ matches any word that precedes a colon and two forward slashes.
-
([^/:]+) captures the domain address part: any sequence of characters that does not include the caret, forward slash, or colon.
-
(:\d*) captures a Web site port number, if it is specified: zero or more digits following a colon.
-
([^# ]*) captures the subdirectory and the page address specified by the Web URI: one or more characters other than # or the space character.
The first backreference will be http, the
second backreference will be www.mindcracker.com, the third backreference will
be :8080, and the fourth backreference will be /myfolder/index.html.
Backreferences allow for strings of data that change slightly from instance to
instance-such as page numbering schemes. We may have a document that numbers
each page with the notation <page n="[some number]" id n="[some chapter name]">;
the number and the chapter name Strings and Arrays 653 change from page to page,
but the rest of the string stays the same. We can write a simple regular
expression that matches these subpatterns:
<page n="\([0-9]+\)" id="\([A-Za-z]+\)">/Page \1, Chapter \2
Buffer number one (\1) holds the first matched sequence, ([0-9]+); buffer number
two (\2) holds the second, ([A-Za-z]+).

Comments
Join the conversation! Your thoughts help the community grow.