sorton9999
sorton9999

Reputation: 182

Latitude/Longitude RegEx in N-S and E-W format

I'm asking what looks like a similar question to all the lat/lon regex questions, but my question puts a different spin on the format that I haven't been able to find. I want to only accept a format as such:

LAT: XX-XX.XX N|S
LON: XXX-XX.XX E|W

This is for a C# window text entry where latitude and longitude are entered in separate textboxes.

I want the format to only accept 1 dash(-) and 1 decimal in those locations (i.e. negative values are invalid) and enforce the range correctly so that all place values need to be entered such as:

LAT 0-90 North or South

00-00.00 N     valid
5-00.00 N      invalid
05-00.00 N     valid
90-00.00 N     valid
89-59.99 S     valid
90-60.00 S     invalid
91.00.00 N     invalid

LON 0-180 East or West

0-0.0 E        invalid
15-00.00 E     invalid
015.00.00 E    valid
180-00.00 E    valid
180-01.00 E    invalid
179-59.99 W    valid
179-60.00 W    invalid
181-00.00 W    invalid

I know how to do it digit by digit such as for Latitude: [0-9][0-9]-[0-5][0-9].[0-9][0-9] [N|S]

That is the extent of my knowledge of RegEx authoring.

As always, any help on this would be much appreciated.

Upvotes: 1

Views: 1519

Answers (4)

zessx
zessx

Reputation: 68790

To limit degrees to 0-90/0-180, and seconds to 0-0/59-99, I would go for these regex:

#Latitude:
(([0-8]\d)-(0\d|[1-5]\d)\.\d\d|90-00.00)\s[NS]
#Longitude:
((0\d\d|1[0-7]\d)-(0\d|[1-5]\d)\.\d\d|180-00.00)\s[EW]

Upvotes: 0

user557597
user557597

Reputation:

This may be hard to look at ... but it validates.

 #  @"(?:(?:(?:(?:0\d|[1-8]\d)(?=-\d\d\.\d\d[ ][NS])|(?:0\d\d|1[0-7]\d)(?=-\d\d\.\d\d[ ][EW]))-(?:(?:(?:0\d|[1-5]\d)\.\d\d)))|(?:(?:90(?=-\d\d\.\d\d[ ][NS])|180(?=-\d\d\.\d\d[ ][EW]))-00\.00))[ ][NSEW]"

 (?:                                 #
      (?:                                 # =============
           (?:                                 #
                (?:                                 # LAT:  00 to 89   North or South
                     0 \d                                #
                  |  [1-8] \d                            #
                )                                   #
                (?= - \d\d \. \d\d [ ] [NS] )
             |                                    # or,

                (?:                                 # LON:  000 to 179   East or West
                     0 \d\d                              #
                  |  1 [0-7] \d                          #
                )                                   #
                (?= - \d\d \. \d\d [ ] [EW] )
           )                                   #
                                               #
           -                                   # -
                                               #
           (?:                                 #
                (?:                                 #
                     (?:                                 # 00 to 59
                          0 \d                                #
                       |  [1-5] \d                            #
                     )                                   #
                     \.                                  # .
                     \d\d                                # 00 to 99
                )                                   #
           )                                   #
      )                                   #

   |                                    # or,

      (?:                                 # =============
           (?:                                 #
                90                                  # LAT:  90  North or South    
                (?= - \d\d \. \d\d [ ] [NS] )
             |                                    # or,
                180                                 # LON: 180  East or West
                (?= - \d\d \. \d\d [ ] [EW] )
           )                                   #
           - 00 \. 00                          # - 00.00
      )                                   #

 )                                   #
 [ ]                                 #  =============
 [NSEW]                              # N,S,E,W

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

My suggestion is

String patternLatitude = @"^(90\-00\.00)|([0-8]\d\-[0-5]\d\.\d\d) (N|S)$";
String patternLongitude = @"^(180\-00\.00)|((1[0-7]\d)|(0\d\d)\-[0-5]\d\.\d\d) (W|E)$";

providing that the given example

  015.00.00 E    valid 

should be actually invalid. More testing examples (all invalid)

 090-00.01 N
 180-00.01 E
 190-00.00 E
 200-00.00 E

Explanation:

Latitude: 90-00.00 is a special case (the only possible value with 90 degree), for other degree values we can put down [0..8]\d; minutes are [0..5]\d and decimals are just two digits: \d\d.

Longitude: 180-00.00 is a special case (the only possibility with 180 degree); the second case 1** lattitudes: since we don't have 180 or 190 lattitudes we can put them as 1[0-7]\d; finally if a lattitude starts with 0 it can have any two digits more: 0\d\d. Minutes and their decimals are the same as they are in the Lattitude case.

Upvotes: 3

David Arno
David Arno

Reputation: 43254

The following will give you a regex to match the latitudes, along with capturing groups to enable access to the degs/mins/secs and N/S values:

(([0-8]\d)[-.]([0-5]\d)\.(\d\d)|(90[-.]00\.00)) ([N|S])

And the same for E/W:

((0\d\d|1[0-7]\d)[-.]([0-5]\d)\.(\d\d)|(180[-.]00\.00)) ([E|W])

Upvotes: 0

Related Questions