No, you can’t. At least not in general. Regular expressions describe ordinary languages, and they are characterized by the fact that they cannot contain arbitrary nested expressions. So something like
(ab(?:cd)e(fg))
it’s almost impossible to check with regular expressions only. While some regex flavors allow for recursive convergence in a match (e.g. Perl) or balanced capture groups that can mimic this to some extent, this is definitely not a tool designed for this job, and you should not try to train it alone.
What you can do is simply try to compile the expression you want to test. The .NET regular expression engine throws an exception if the template is invalid:
var compiledRegex = new Regex(someString);
source
share