string.Format, regex + curly braces (C#)

c#, regex

Solution

Replace single curly braces with double curly braces:

string regex = string.Format(@"^\d{{0,{0}}}", MaxLength);

If that makes your eyes hurt you could just use ordinary string concatenation instead:

string regex = @"^\d{0," + MaxLength + "}";

Problem

How do I use string.Format to enter a value into a regular expression, where that regular expression has curly-braces in it already to define repetition limitation? (My mind is cloudy from the collision for syntax) e.g. The normal regex is "^\d{0,2}", and I wish to insert the '2' from the property MaxLength

Original source