Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Using backreferences in a Match

You can reference backreferences within a regular expression to keep the length and complexity of patterns to a minimum.  Backreferences can be referenced with the \1..\N or \k'name' notation.  To highlight where this is useful consider a simple-minded pattern for matching Dates.  Dates can (potentially) be delimited by either a space, a dot, a hyphen or a slash - that is, any of the following might be allowed:

1-1-11
1.1.11
1/1/11
1 1 11

But this wouldn't:

1.1-11

Rather than writing four separate expressions to handle the variations, you can perform one match on [-\.\/ ] and then use either \1..\N or \k'name' to reference the matched value, to demonstrate this let's look at an example:

\d{1,2}
([-\.\/ ])
\d{1,2}
\1
\d{2,4}

or...

\d{1,2}
(?'delim'[-\.\/ ])
\d{1,2}
\k'delim'
\d{2,4}

This method could also be useful for matching markup attributes where you don't know whether the delimiter character will be a ' or a " and can also be applied to matching doubled-up characters, words or phrases.

No Comments