I'm building an application that saves named regular expressions into a database. Looks like this:
I'm using Asp.Net forms. How can I validate the entered regex? It would like the user to know if the entered regex is not a valid .Net regular expression.
The field should reject values like:
^Wrong(R[g[x)]]$
Invalid\Q&\A
Make new Regex class out of it. If it throws exception, then it is invalid.
try{
new Regex(expression)
}
catch(ArgumentException ex)
{
// invalid regex
}
// valid regex
I know. Using exceptions for code logic is wrong. But this seems to be only solution.
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