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
You can now use string interpolation to do it:
string regex = $@"^\d{{0,{MaxLength}}}";
Again, you need to escape the curly braces by doubling them.
You can escape curly braces by doubling them :
string.Format("Hello {{World}}") // returns "Hello {World}"
In your case, it would be something like that :
string regexPattern = string.Format("^\d{{0,{0}}}", MaxLength);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With