I have a question out of my own curiosity. Although I have a working solution, I am interested to know why one method is successful while the other isn't.
I was in need of a regular expression for validating if a user input is a valid number. Here are some examples:
87
887.65
-87
-87.65
My initial attempt was like this:
^(\-?[0-9]+(\.[0-9]*)?)$
It worked well, but it accepted strings like '7x', '1a', '89p' which were not desired. My updated solution is as follows and seems to be working correctly:
^(\-?[0-9]+(\.[0-9]+)?)$
The second one (with the '+') is more concise, yet I'm unsure why the first one allowed letters and the second did not. Can anyone see what I might be overlooking?