If you are allowed to modify the regular expression, you must surround it with ^( ... )$ . You can do this at runtime as follows:
string newRe = new Regex("^(" + re.ToString() + ")$");
Brackets are needed here to prevent the creation of a regular expression such as ^a|ab$ that will not do what you want. This regular expression matches any line starting with a or any line ending with ab .
If you do not want to change the regex, you can check Match.Value.Length == input.Length . This is the method used in ASP.NET regex validators. See my answer here for a more complete explanation.
Please note that this method may cause some interesting problems that you should be aware of. The regular expression "a | ab" will match the string "ab", but the match value will only be "a". Therefore, although this regular expression could match the entire string, it is not. The documentation has a warning about this.
source share