Comment On Irregular Expressions

It's a pretty common occurrence to see a programmer completely disregard regular expressions, instead implementing his own validating code. A handful of the Do-It-Your-Selfers will even implement their own substring searching algorithm with all sorts of varieties of loops and the like. David Conrad's colleague managed to not only skip the built-in parseFloat method, but use regular expressions, an array, and a loop to do so ... [expand full text]
« PrevPage 1 | Page 2Next »

Re: Irregular Expressions

2005-11-30 15:29 • by asdf
I love reinventing the wheel!

Re: Irregular Expressions

2005-11-30 15:29 • by rogthefrog
That's just splendid. At least s/he breaks at the first occurrence of an invalid char.



Oh and FIRST!!!!!!111111

Re: Irregular Expressions

2005-11-30 15:30 • by rogthefrog
52194 in reply to 52193
rogthefrog:
That's just splendid. At least s/he breaks at the first occurrence of an invalid char.



Oh and FIRST!!!!!!111111




I swear I had the first post. DAMMIT

Re: Irregular Expressions

2005-11-30 15:30 • by Mark
Cool.  ".-." is a number.

Re: Irregular Expressions

2005-11-30 15:31 • by test1

woo hoo, ellipses are numbers... 

Re: Irregular Expressions

2005-11-30 15:32 • by Colin
According to the msdn link:



parseFloat("1.2abc")   // Returns 1.2.


which is not what the code above does. The above code would return false when it sees the 'a'....

Re: Irregular Expressions

2005-11-30 15:33 • by jkl;
52198 in reply to 52195
Anonymous:
Cool.  ".-." is a number.




so is 8-0



it's almost an emoticon

Re: Irregular Expressions

2005-11-30 15:45 • by BlackTigerX

...and it should return


true


falue


NaN

Re: Irregular Expressions

2005-11-30 15:45 • by Gene Wirchenko
52200 in reply to 52195
Anonymous:
Cool.  ".-." is a number.




Equal to 0, right?




This is not totally unselective code though.  According to it, +5 is not a number.




Sincerely,





Gene Wirchenko



Oh, come on!

2005-11-30 15:48 • by sammybaby

Clearly, you are all missing the utter brilliance of this function.


Everyone with more than a little experience with regular expressions knows that in some cases, they need to be constructed carefully to avoid becoming resource hungry. Here, the author has cleverly limited the amount of damage this regexp can do by simplifying it to a single list of characters, then iterating over the string containing the number.


The fact that he has enumerated each digit individually rather than using a character reference like \d highlights his dedication to self-documenting code, and ensures that if those godless heathen who support commie pinko trash like the Metric system ever succeed in adding new digits to our numbering system. It's poetry. 

Re: Irregular Expressions

2005-11-30 15:50 • by Zen Bastard
52202 in reply to 52197
Anonymous:
According to the msdn link:



parseFloat("1.2abc")   // Returns 1.2.


which is not what the code above does. The above code would return false when it sees the 'a'....




If that's true, then the only WTF I see here is that David Conrad
thinks he's smarter than his colleague. But, I'm pretty much a n00b.
All I see here is a straightforward, well
commented function that will validate a float. Am I wrong?

Re: Irregular Expressions

2005-11-30 15:50 • by rbriem
52203 in reply to 52198

Anonymous:
Anonymous:
Cool.  ".-." is a number.


so is 8-0

it's almost an emoticon


Almost?


I think that's the DailyWTF emoticon mascot ...

Re: Oh, come on!

2005-11-30 15:50 • by sammybaby
52204 in reply to 52201

My apologies. The above should have read:


sammybaby:


The fact that he has enumerated each digit individually rather than using a character reference like \d highlights his dedication to self-documenting code, and ensures that if those godless heathen who support commie pinko trash like the Metric system ever succeed in adding new digits to our numbering system, his code shall remain unsullied by their taint. It's poetry.



 

Re: Irregular Expressions

2005-11-30 15:53 • by boa13
52205 in reply to 52202
Anonymous:
All I see here is a straightforward, well
commented function that will validate a float. Am I wrong?




Yes. It does not validate a float, as many other comments have pointed
out. ".-.---." is accepted, but "+5.3" is rejected, for example.

Re: Irregular Expressions

2005-11-30 15:54 • by sammybaby
52207 in reply to 52202
Anonymous:
Anonymous:
According to the msdn link:

parseFloat("1.2abc")   // Returns 1.2.


which is not what the code above does. The above code would return false when it sees the 'a'....



If that's true, then the only WTF I see here is that David Conrad thinks he's smarter than his colleague. But, I'm pretty much a n00b. All I see here is a straightforward, well commented function that will validate a float. Am I wrong?


Regrettably, you are wrong. The code in today'sWTF will also interpret Morse Code as a valid Float.


testFloat("...---...")    // Returns true

Re: Irregular Expressions

2005-11-30 15:58 • by Grimoire
52208 in reply to 52192
Anonymous:
I love reinventing the wheel!

Especially when you reinvent it square...

Re: Irregular Expressions

2005-11-30 15:58 • by rbriem
52209 in reply to 52203

Unless I'm mistaken (happened once), wouldn't ";;;;;" be a number too?

Re: Irregular Expressions

2005-11-30 15:58 • by Volmarias
52210 in reply to 52207
Silliest ascii art that can be validated by this code contest!

I'll start with a bridge!

.1----1.

Re: Irregular Expressions

2005-11-30 16:01 • by clyde
52211 in reply to 52207

testFloat("...---...") should throw a UserHasFallenAndCantGetUpException.

Re: Oh, come on!

2005-11-30 16:03 • by Nicholas Shanks
52212 in reply to 52201
Add more digits to our system, like when A-F and x were added (like 0xAD, for example ;) ).

Re: Irregular Expressions

2005-11-30 16:08 • by Salta Mada
52214 in reply to 52199
BlackTigerX:

...and it should return


true


falue


NaN



No. It should return...

True
False
FileNotFound

Re: Irregular Expressions

2005-11-30 16:10 • by MikeB
Woo Hoo! I'm first!

Re: Irregular Expressions

2005-11-30 16:14 • by Disgruntled DBA
I knew my Social Security number was a number.

Re: Irregular Expressions

2005-11-30 16:20 • by bit
52219 in reply to 52207
sammybaby:
Anonymous:
Anonymous:
According to the msdn link:

parseFloat("1.2abc")   // Returns 1.2.


which is not what the code above does. The above code would return false when it sees the 'a'....



If that's true, then the only WTF I see here is that David Conrad thinks he's smarter than his colleague. But, I'm pretty much a n00b. All I see here is a straightforward, well commented function that will validate a float. Am I wrong?


Regrettably, you are wrong. The code in today'sWTF will also interpret Morse Code as a valid Float.


testFloat("...---...")    // Returns true



As a bonus, your Morse message would also reflect the truth... :)

.-- - ..-. !!!

Re: Oh, come on!

2005-11-30 16:27 • by Anonymoose
52221 in reply to 52201
sammybaby:

... and ensures that if those godless
heathen who support commie pinko trash like the Metric system ever
succeed in adding new digits to our numbering system.





Guh?  Are you referring to the base 10 metric system?  What about using base 12 for counting inches in a foot, huh?



And then, you are forgetting those HREF="/forums/47705/ShowPost.aspx">crackpots
who have added the letters A through F.





Re: Irregular Expressions

2005-11-30 16:30 • by richleick
While we are reinventing the wheel, I thought I would share this little
function that was written for a common C library.  It's been a
while, so forgive any syntax errors.  But I'm sure you get the
point.



void setNull(char *variable)

{

   variable = null;

}



I always love coming across home grown functions like checkDate or
checkNumeric that were written to somehow make the existing isDate()
and isNumeric() functions irrelavant and obsolete.

Re: Irregular Expressions

2005-11-30 16:35 • by Reun
Alex Papadimoulis:
  var theRegExp = /[0;1;2;3;4;5;6;7;8;9;.;-]/





I don't use the language in question, but WTF? What kind of regular expression is that?

Re: Irregular Expressions

2005-11-30 16:38 • by Dave
52224 in reply to 52223

I like the way it allows several smilies as numbers.


testNum(";-0")


testNum("8-0")


 

Re: Irregular Expressions

2005-11-30 16:50 • by ammoQ
I guess the first version of the function was written straight-forward (with all sorts of varieties of loops and the like). E.g.




validChars = "0123456789.-";

...

isValid = (validChars.indexOf(theChar)>-1);






Then someone more clever came along, saw it and said:
"Stupid! You can do that much better with regular expressions!"
Said that and went along.
And poor little stupid programmer read the first 10 lines of the first chapter
of a book about regular expressions and used that knowledge to adopt
his function to use regular expressions.

Re: Irregular Expressions

2005-11-30 16:52 • by Omnifarious

Not only did the programmer fail to use a standard library routine, the programmer was clearly ignorant of what regular expressions could do.


This would've caught many more invalid inputs and been much faster (depending on the exact semantics of search, maybe there's a match function that should be used instead.:


function testNum(strNum)

{
var the theRegExp = /^(-)?[0-9]+(\.[0-9]+)?$/;
return strNum.search(theRegExp) != -1;
}


I can see a reason to write your own validating function instead of relying on the system float parser as you may not want people appending random stuff to the values that gets ignore, or allow them to specify exponents or whatever. But, yeah, it's pretty obvious what's wrong with this.


Re: Irregular Expressions

2005-11-30 17:04 • by foxyshadis
52227 in reply to 52223
Reun:
Alex Papadimoulis:
  var theRegExp = /[0;1;2;3;4;5;6;7;8;9;.;-]/





I don't use the language in question, but WTF? What kind of regular expression is that?


It's either perl shoehorned into a C syntax, or more likely javascript.



I actually learned something today, which is that JS uses perl-syntax rather than c-strings to hold regexps. Cool. =D



I'm surprised the unescaped - doesn't lead to a parse error, since it
has a different meaning in a character set. And the blatant
misunderstandings of regex sets via the semicolons is <3



I think this guy would have been much more comfortable looping through
an array of characters for every digit in the input instead. It's more
his style.

Now you have to wonder, when "-.-" gets through and shelved into the database, what does he do when the error is flagged? Convert the field to varchar2! And when the parser on the other end dies, because it tries to directly convert to float, the obvious answer is to write a new function to replace microsoft's buggy and unreliable one. Awesome. =D

Re: Irregular Expressions

2005-11-30 17:07 • by Jeremy
52228 in reply to 52197
Anonymous:
According to the msdn link:



parseFloat("1.2abc")   // Returns 1.2.


which is not what the code above does. The above code would return false when it sees the 'a'....


I believe a good version would be:

function testNum(strNum) {
    return !isNaN(strNum)
}

Re: Irregular Expressions

2005-11-30 17:09 • by Vexo
Even better, creating the "charArray" is pointless. JS lets you access chars in a string by index directly.

Re: Irregular Expressions

2005-11-30 17:10 • by loneprogrammer
52230 in reply to 52223
Reun:
Alex Papadimoulis:
  var theRegExp = /[0;1;2;3;4;5;6;7;8;9;.;-]/





I don't use the language in question, but WTF? What kind of regular expression is that?


It is the same as [0123456789;.-] which matches any character between
the brackets.  Having the ; in there multiple times is redundant
but harmless.



;--0   so is a valid number too.



Re: Irregular Expressions

2005-11-30 17:14 • by loneprogrammer
52231 in reply to 52227
foxyshadis:


It's either perl shoehorned into a C syntax, or more likely javascript.



It has to be Javascript.


foxyshadis:


I'm surprised the unescaped - doesn't lead to a parse error, since it
has a different meaning in a character set. And the blatant
misunderstandings of regex sets via the semicolons is <3



If you want a - character inside a character set, it must be either the
first or the last character.  Putting it anywhere in the middle
creates a range of characters (e.g. [a-z-] is any lowercase letter OR a
dash).  All regexp syntaxes do this.



Re: Irregular Expressions

2005-11-30 17:16 • by loneprogrammer
52232 in reply to 52222
richleick:
While we are reinventing the wheel, I thought I would share this little
function that was written for a common C library.  It's been a
while, so forgive any syntax errors.  But I'm sure you get the
point.



void setNull(char *variable)

{

   variable = null;

}


Actually, what is the point?  This function doesn't do anything!  Remember that C functions are pass-by-value.



Re: Irregular Expressions

2005-11-30 17:21 • by anon
^-?\d+.\d+$

How hard was that?

Re: Irregular Expressions

2005-11-30 17:32 • by Anon
52236 in reply to 52234
Too bad you didn't escape the period...



Re: Irregular Expressions

2005-11-30 17:38 • by phx
52237 in reply to 52236
Hey dont knock it! It validates IP addresses too (with the sort of quality it validates numbers) ;)

Re: Irregular Expressions

2005-11-30 17:38 • by don
52238 in reply to 52234
You mean:

^-?\d+(\.\d+)?$

Re: Irregular Expressions

2005-11-30 17:39 • by Maurits
I get it, he forgot to consider exponential notation...

Re: Irregular Expressions

2005-11-30 17:45 • by slainangel
52240 in reply to 52232
loneprogrammer:
richleick:
While we are reinventing the wheel, I thought I would share this little
function that was written for a common C library.  It's been a
while, so forgive any syntax errors.  But I'm sure you get the
point.



void setNull(char *variable)

{

   variable = null;

}


Actually, what is the point?  This function doesn't do anything!  Remember that C functions are pass-by-value.




I've seen this one in a pile of code before.

Some simple WTFs crop up way too often.

Re: Irregular Expressions

2005-11-30 17:51 • by GalacticCmdr
52241 in reply to 52232
loneprogrammer:
richleick:
While we are reinventing the wheel, I thought I would share this little
function that was written for a common C library.  It's been a
while, so forgive any syntax errors.  But I'm sure you get the
point.



void setNull(char *variable)

{

   variable = null;

}


Actually, what is the point?  This function doesn't do anything!  Remember that C functions are pass-by-value.






Yes, that is correct that C is strictly pass-by-value; however he did ask for forgiveness on syntax errors.



He might have meant...




void setNull(char **variable)


{


   *variable = null;


}



Which when called as....



char test = 't';

setNull(&test);



It should put a null into the test variable.



I may be totally off-base on my function since I have not worked in C-land in many years.

Re: Irregular Expressions

2005-11-30 18:17 • by Erin
52245 in reply to 52238
Actually, I believe the correct regex would be:
/^[+-]?(?:\.?\d+|\d+\.?|\d+\.\d+)(?:[eE][+-]?\d+)?$/

Which rather nicely illustrates why you should just use the built-in methods.

Re: Irregular Expressions

2005-11-30 18:18 • by Omnifarious
52246 in reply to 52241
GalacticCmdr:
Yes, that is correct that C is strictly pass-by-value; however he did ask for forgiveness on syntax errors.



He might have meant...




void setNull(char **variable)


{


   *variable = null;


}



Which when called as....



char test = 't';

setNull(&test);



It should put a null into the test variable.



I may be totally off-base on my function since I have not worked in C-land in many years.

I believe most C compilers will error out on that code. &test is of type char *, not char **.


char *test = "t";

setNull(&test);

The above would work. :-)

Re: Irregular Expressions

2005-11-30 18:22 • by Omnifarious
52247 in reply to 52245
Anonymous:
Actually, I believe the correct regex would be:
/^[+-]?(?:\.?\d+|\d+\.?|\d+\.\d+)(?:[eE][+-]?\d+)?$/

Which rather nicely illustrates why you should just use the built-in methods.

So, what do you do when you don't want to allow exponential notation and force a leading 0 before the . and not allow a leading +? There are reasons to not allow complicated floats in a field. I don't think writing your own float validator is necessarily the wrong thing, but this one is a very broken float validator.


Re: Irregular Expressions

2005-11-30 18:24 • by Fizzl
52248 in reply to 52232
loneprogrammer:
richleick:
While we are reinventing the wheel, I thought I would share this little
function that was written for a common C library.  It's been a
while, so forgive any syntax errors.  But I'm sure you get the
point.




void setNull(char *variable)

{

   variable = null;

}


Actually, what is the point?  This function doesn't do anything!  Remember that C functions are pass-by-value.






My thoughts exactly. it should be "void setNull(char &variable)" to pass by reference.

I shut (shat? wtf, english preparser is broken) my hole earlier because
I thought I was too drunk to remember my primary language.

.

..

...

....

Althou it is still mighty possible that I am too drunk to remember my primary language. Whatever that might be.

Which reminds me, was there a convention in Visual C++ 6< to use NULL as an object for null? (just a matter of case)

Re: Irregular Expressions

2005-11-30 18:36 • by Fizzl
52250 in reply to 52248
Fizzl:
loneprogrammer:
richleick:
While we are reinventing the wheel, I thought I would
share this little
function that was written for a common C library.  It's been a
while, so forgive any syntax errors.  But I'm sure you get the
point.




void setNull(char *variable)

{

   variable = null;

}


Actually, what is the point?  This function doesn't do anything!  Remember that C functions are pass-by-value.






My thoughts exactly. it should be "void setNull(char &variable)" to pass by reference.

I shut (shat? wtf, english preparser is broken) my hole earlier because
I thought I was too drunk to remember my primary language.

.

..

...

....

Althou it is still mighty possible that I am too drunk to remember my primary language. Whatever that might be.

Which reminds me, was there a convention in Visual C++ 6< to use NULL as an object for null? (just a matter of case)




And now, after seeing the other comments about how to handle a nulling
off a parameter pointer (pointer parameter?), I have to withdraw to my
cave and produce 2.000 lines of test code to illustrate how the
different cases of c/c++ pointer passing cases work. I'll be back in
three weeks after I have specified this project. At that point I will
file for Request For Comments to start the actual implementation of the
trial.



PS. Where's the fucking preview button?! This site is a big WTF in itself.

Re: Irregular Expressions

2005-11-30 19:46 • by trollable
I finally understood why this code is bad: multiple return statements.

Never break the flow. A real WTF ;)

Re: Irregular Expressions

2005-11-30 20:14 • by triso
52253 in reply to 52203
rbriem:

Anonymous:


so is 8-0

it's almost an emoticon



Almost?


I think that's the DailyWTF emoticon mascot ...



Looks more like Elton John to me.  Big horn-rimmed glasses and a gaping maw.



« PrevPage 1 | Page 2Next »

Add Comment