You can store a part of a matched pattern for later reuse. Placing parentheses around a Regular Expression pattern or part of a pattern causes that part of the expression to be stored into a temporary buffer.
Each captured sub-match is stored as it is encountered from left to right in a Regular Expressions pattern. The sub-matches are numbered beginning at 1 and continuing up to a maximum of 99 sub-matches. Each different buffer can be accessed using
\nwhere n is one or two decimal digits identifying a specific buffer, e.g. \1.
For example, back-references can be used to check for double occurrences of the same words, e.g. in a string such as:
Scripting is is fun!The following JScript .NET Regular Expression uses a single sub-expression to check for duplicates:
/\b([a-z]+) \1\b/gimThe sub-expression is everything between parentheses. That captured expression includes one or more alphabetic characters, as specified by [a-z]+. The second part of the Regular Expression (\1) is the reference to the previously captured sub-match, that is, the second occurrence of the word. \b is used for word boundary, so that the check is done on complete words.