Code readability is always good, however there are times when performance is
more important than readability, and with extensive testing one can
dramatically reduce the risk of such a complicated regular expression from
causing you issues.
While using a Parse() may be simpler and more readable, I disagree that it
is more efficient. Usage of either depends on which side you want to pay the
penalty of an unparseable value.
If it is a parseable value being passed in, then yes, Parse() faster, an
order of magnitude faster in the best case. On the other hand, if a lot of
invalid values are being dealt with, using the Regex method can be in the
best case ~50 times faster than Parse().
Brendan
"Jon Skeet [C# MVP]" wrote:
[color=blue]
> Brendan Grant <grantb@NOSPAMdahat.com> wrote:[color=green]
> > Try these two functions. The first tells you if the string passed in is a
> > number (int or float), while the second just tells you if it is an integer.[/color]
>
> I find that complicated regular expressions, when unnecessary,
> seriously detract from readability and could easily lead to subtle
> bugs.
>
> They also don't perform as well as "brute force" hard coded tests in
> simple cases.
>
> Using a hard-coded test followed by int.Parse (or whatever parse) is
> more readable, simpler, and more efficient than using regular
> expressions.
>
> (The difference in terms of performance is quite impressive, although
> of course it'll only be significant in extreme cases where you're
> parsing millions of the things.)
>
> Regexes are very powerful, when used in the right place - but I believe
> in this case it's like boarding an aeroplane in order to travel a
> hundred metres.
>
> --
> Jon Skeet - <skeet@pobox.com>
>
http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>[/color]