I’ve been using this C# function to determine if the passed string could be interpreted as a valid regex.
public static bool IsValidRegex(string regexCandidate)
{
string validReRe = @"^(?:(?:[^?+*{}()[]\|]+|\.|[(?:^?\.|^[^\]|[^\^])(?:[^]\]+|\.)*]|((?:?[:=!]|?<[=!]|?>|?<[^Wd]w*>|?'[^Wd]w*')?(?<N>)|)(?<-N>))(?:(?:[?+*]|{d+(?:,d*)?})[?+]?)?||)*$(?(N)(?!))";
Regex rere = new Regex(validReRe);
MatchCollection mc = rere.Matches(regexCandidate);
return mc.Count > 0;
}
For example “cba] – abc” would return false, because the opening ‘[‘ is missing.
I have to admit I found this regex on the internet and never gave it much thought as to how it works.
I’ve noticed that the function gets really slow as the length of the left hand part of this input increases.
For example, pass it “vutsrqponmlkjihgfedcba] – abc” and it will take the function 3 seconds on my system to come to the conclusion that it is not a valid regex.
With every character that I add on the left hand side, the run time seems to almost double.
Why is this?
Could the function be modified in a way so that is doesn’t pose this problem? Or is there a better way altogether to determine if a string could be interpreted as a regex?