TangoMike
TangoMike

Reputation: 133

Regex lat lng format

I have the following regex to test for decimal lat lng:

^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),?[,\s]{1}[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$

This works well with lat/lng in the formats:

53.58465, 113.50034
53.58465 113.50034

However, I caught this in my log file today and unsure how to change the regex to optionally include the following:

53.58465° N, 113.50034° W
53.58465° N 113.50034° W   // <--- No comma
53.58465°N, 113.50034°W
53.58465°N 113.50034°W   // <--- No comma

Upvotes: 1

Views: 44

Answers (1)

trincot
trincot

Reputation: 350137

You could allow for each of these extra characters ("°",space,"N","W",comma) to optionally be included in the input at their expected spot:

^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)°?\s?N?,?\s?[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)°?\s?W?$

Upvotes: 1

Related Questions