asp.net.ph

Skip Navigation LinksGetting Started > ASP.NET Base Types > Regular Expressions > Details of Regular Expression Behavior > Backreferences

Backreferences

Getting Started   ASP.NET Base Types   Details of Regular Expression Behavior


Parsing Backreferences

The expressions \1 through \9 always refer to backreferences, not octal codes. Multidigit expressions \11 and up are considered backreferences if there is a backreference corresponding to that number; otherwise, they are interpreted as octal codes ( unless the starting digits are 8 or 9, in which case they are treated as literal "8" and "9" ). If a regular expression contains a backreference to an undefined group number, it is considered a parsing error. If the ambiguity is a problem, you can use the \k<n> notation, which is unambiguous and cannot be confused with octal character codes; similarly, hexadecimal codes such as \xdd are unambiguous and cannot be confused with backreferences.

Backreference behavior is slightly different when the ECMAScript option flag is enabled. For more information, see ECMAScript vs. Canonical Matching Behavior.

Matching Backreferences

A backreference refers to the most recent definition of a group ( the definition most immediately to the left, when matching left to right ). Specifically, when a group makes multiple captures, a backreference refers to the most recent capture. For example, ( ?<1>a ) ( ?<1>\1b ) * matches aababb, with the capturing pattern ( a ) ( ab ) ( abb ). Looping quantifiers do not clear group definitions.

If a group has not captured any substring, a backreference to that group is undefined and never matches. For example, the expression \1 ( ) never matches anything, but the expression ( ) \1 matches the empty string.

See Also

Regular Expressions



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note