I’m coming from a
UNIX/shell/Perl background, but this should apply to other regex applications as well.
Using egrep-style Extended Regular Expressions (ERE’s):
first check that input is not null/blank
(shouldn’t require regex)
check that input doesn’t contain characters other than numbers, decimal points, and minus signs
regex:
[^0-9BACKSLASH.BACKSLASH-]+
(replace BACKSLASH with a you-know-what, this posting system don’t like BACKSLASHES)
and then check for valid numeric format
regex:
[-]?[0-9]*[.]{0,1}[0-9]{0,4}
the above regex checks for (in this order): starts with exactly one or none minus sign(s), followed by a numeric string (or not), followed by exactly one decimal point or no decimal point, followed by a numeric string of length 0-4
some valid input:
-19.908
80.847
-180
1.0
0.0999
.0999
179.9999
for stricter formatting of the fractional decimal value, use:
[-]?[0-9]*[.]{0,1}[0-9]{4}
valid input:
.0999
-0.1234
-90.0000
179.9999
and then check for appropriate value range with greater than less than operators
(shouldn’t require regex)
Hope that helps. Let me know if I’m way off.
For some more help, here is an awesome regex reference:
http://www.regular-expressions.info/
and an interactive regex tester (for javascript)
http://www.regular-expressions.info/javascriptexample.html
-kelly