.
  Lightstalkers
* My Profile My Galleries My Networks

Regex for decimal coordinates

Does anyone know how to write a regular expression that would validate decimal latitude and longitude? I stink at regex.

by Shinji Kuwayama at 2005-01-17 14:25:34 UTC (ed. Mar 12 2008 ) | Bookmark | | Report spam→

ooh ooh, I was waiting for someone to ask about regex’s! What flavor of regex? What are the parameters for lat/long ranges? (i guess I could look it up).
I’m doing a lot of regex’s lately.

by Kelly Chang | 17 Jan 2005 14:01 | | Report spam→
Holy cow, for real? I have to post more questions!

Ok, here are the valid ranges:

Latitude: -90.0000 to 90.0000

Longitude: -180.0000 to 180.0000

Not too bad, right?

by Shinji Kuwayama | 17 Jan 2005 14:01 | | Report spam→
quick questions:

a.) What are you programming in? (shell, perl, php, java. . .)
b.) Why regex instead of specifying a numerical range? (Depends on answer to the first question I guess)

I ask because regex can be overly complicated for values, but great for other text.

in the meantime, I’m working on it. . .

by Kelly Chang | 18 Jan 2005 02:01 | | Report spam→
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

by Kelly Chang | 18 Jan 2005 04:01 (ed. Jan 18 2005) | | Report spam→
Ah, this is excellent! Thank you so much Kelly; this is exactly the instruction I needed. Thanks to you the map is going to be much more populous from now on… :-)

by Shinji Kuwayama | 21 Jan 2005 16:01 (ed. Jan 21 2005) | | Report spam→

Get notified when someone replies to this thread:
Feed-icon-10x10 via RSS
Recommended
Icon_email via email
You can unsubscribe later.

More about sponsorship→

Participants

Shinji Kuwayama, Software Engineer Shinji Kuwayama
Software Engineer
Chicago , United States ( ORD )
Kelly Chang, Kelly Chang
New York , United States


Keywords

Top↑ | RSS/XML | Privacy Statement | Terms of Use | support@lightstalkers.org / ©2004-2014 November Eleven