In my ASP.net project I need to validate some basic data types for user inputs. The data types are like numeric, decimal, datetime etc.
What is the best approach that I should have taken in terms of performance? Is it to do it by Regex.IsMatch()
or by TryParse()
?
Thanks in advance.
String operations will always be faster than regular expression operations. Unless, of course, you write the string operations in an inefficient way. Regular expressions have to be parsed, and code generated to perform the operation using string operations.
Regex. Split is more capable, but for an arrangement with basic delimitting (using a character that will not exist anywhere else in the string), the String. Split function is much easier to work with. As far as performance goes, you would have to create a test and try it out.
Regex is faster for large string than an if (perhaps in a for loops) to check if anything matches your requirement.
Good regular expressions are often longer than bad regular expressions because they make use of specific characters/character classes and have more structure. This causes good regular expressions to run faster as they predict their input more accurately.
TryParse
and Regex.IsMatch
are used for two fundamentally different things. Regex.IsMatch
tells you if the string in question matches some particular pattern. It returns a yes/no answer. TryParse
actually converts the value if possible, and tells you whether it succeeded.
Unless you're very careful in crafting the regular expression, Regex.IsMatch
can return true
when TryParse
will return false
. For example, consider the simple case of parsing a byte
. With TryParse
you have:
byte b;
bool isGood = byte.TryParse(myString, out b);
If the value in myString
is between 0 and 255, TryParse
will return true
.
Now, let's try with Regex.IsMatch
. Let's see, what should that regular expression be? We can't just say @"\d+"
or even @\d{1,3}"
. Specifying the format becomes a very difficult job. You have to handle leading 0s, leading and trailing white space, and allow 255
but not 256
.
And that's just for parsing a 3-digit number. The rules get even more complicated when you're parsing an int
or long
.
Regular expressions are great for determining form. They suck when it comes to determining value. Since our standard data types all have limits, determining its value is part of figuring out whether or not the number is valid.
You're better off using TryParse
whenever possible, if only to save yourself the headache of trying to come up with a reliable regular expression that will do the validation. It's likely (I'd say almost certain) that a particular TryParse
for any of the native types will execute faster than the equivalent regular expression.
The above said, I've probably spent more time on this answer than your Web page will spend executing your TryParse
or Regex.IsMatch
--total throughout its entire life. The time to execute these things is so small in the context of everything else your Web site is doing, any time you spend pondering the problem is wasted.
Use TryParse
if you can, because it's easier. Otherwise use Regex
.
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