which kind of IsHex() function do you like most, and why?
Posted by jpluimers on 2012/07/10
Though the sample question is in C#, it applies to almost any language and framework: for relatively simple checks like IsHex(), you can go the RegEx way, or the compound if-statement way.
Which kind of function do you like most?
I’m not only interested in the percentages, so let me know in the comments why.
–jeroen
PS: if you want to use RegEx in .NET, you can compile them to IL, but be very cautious for the compilation overhead.






WarrenP said
The gratuituous use of Regular Expressions should get you banished from Delphi land, and you should be forced to write everything for 5 years using Perl, or PHP, the two scripting languages I abhor most.
W
Eric said
RegEx saves typing, but that’s about the only thing going for it in that case.
And if the languages supports “in” like Pascal does, then the RegEx doesn’t save very much code.
for c in str do
if not c in [‘0’..’9′, ‘a’..’f’, ‘A’..’F’] then Exit(False)
Result:=True;
EMB said
Also, faster and readable, IMHO.
Jolyon Smith said
What Danny said. :)
Danny Thorpe (@danny_thorpe) said
If expressions over regex. Two reasons: discrete logic is easier to debug than regex codes, and you don’t need monster truck to cross a street. Regex is overkill.
Dk said
all said.
Jacob said
Long before there were programmers, carpenters have said “When all you have is a hammer, everything looks like a nail.”
Somehow, for programmers, regular expressions have become the hammer.
jpluimers said
You nailed it (:
I was wondering why so many answers on stackoverflow, newsgroups, etc include RegEx even for things that can be done in a simple if/then or case/switch way.