0

I want to ask if the following two regular expressions are good for describing coordinates. Because latitude has just 90 degrees and longitude 180, I have created two seperate regular expressions:

Latitude regex:

\A[+-]?((9[0]?|[0-8][0-9]?([.,][0-9]+)?))\z

Regular expression visualization

Debuggex Demo

Longitude regex:

\A[+-]?(180|(1[0-7][0-9]|[0-9]{1,2})([.,][0-9]+)?)\z

Regular expression visualization

Debuggex Demo

I've tested them with some coordinates in Rubular. I wanted to ask if I miss something important that wouldn't describe valid coordinates.

Tobias
  • 4,523
  • 2
  • 20
  • 40
  • http://stackoverflow.com/questions/3518504/regular-expression-for-matching-latitude-longitude-coordinates – Alexander Shlenchack Dec 17 '15 at 10:17
  • Yes I've seen this but I wanted to have a more general regex without the special metacharacters of ruby – Tobias Dec 17 '15 at 10:35
  • 1
    Longitude is wrong: http://www.rubular.com/r/Af7AcJIxuV – Aleksei Matiushkin Dec 17 '15 at 11:28
  • Hi @mudasobwa you are right! Do you know a solution? – Tobias Dec 17 '15 at 12:01
  • I've updated the longitude regex now. Thanks to @mudasobwa again! – Tobias Dec 17 '15 at 12:11
  • Tobias, you should provide the input you want to match – Thomas Ayoub Dec 17 '15 at 12:46
  • Hi @Thomas! As I said it should match different types of coordinates. For example +180, -180, 19.1912, etc. But I don't know if there is something additional to care for what I forgot in my regexes. – Tobias Dec 17 '15 at 12:49
  • So you want us to (try to) take down your regex, that's it? – Thomas Ayoub Dec 17 '15 at 12:50
  • If you see something that doesn't describe all types of latitudes or longitudes, yes. I don't want you to go and try a lot of different coordinates. I just wanted to know if people with some more experience with geographic coordinates see something that isn't correct for coordinates. – Tobias Dec 17 '15 at 13:04
  • 1
    Regular expressions can be notoriously hard to read. In ruby you can break regular expressions on multiple lines and add comments to make it easier on yourself and your fellow programmers. – R. Peereboom Dec 17 '15 at 14:33
  • Thanks @R.Peereboom for the info! How would you break a regular expression on multiple lines? Always when I start a subexpression? – Tobias Dec 17 '15 at 14:36
  • @Tobias It depends, you can find a rather terrible example in the [ruby Regexp docs](http://ruby-doc.org/core-2.2.4/Regexp.html#class-Regexp-label-Free-Spacing+Mode+and+Comments). Here is a lattitude example, you might have to copy paste in a text editor (I've numbered the lines): 1.\A # start of string 2.[+-]? # optional sign 3.( 4.(9[0]?|[0-8][0-9]?# Number upto 90 (can be simplified) 5.([.,][0-9]+)?) # optional fraction 6.) 7.\z # end of string – R. Peereboom Dec 17 '15 at 19:22

1 Answers1

0

The Latitude regex does not suit the picture and its Debuggex Demo; while picture and demo are correct, the shown regex fails to match e. g. 9.0.
The Longitude regex and its picture and demo are correct.

Armali
  • 18,255
  • 14
  • 57
  • 171