Pattern: GPS coordinates

“GPS coordinates” is one of the patterns that you can select on the Match panel. Use this pattern to make a field match GPS coordinates. The pattern provides many different options to provide very comprehensive support for all the different notations that can be used for GPS coordinates. Ticking all the options makes the regex as flexible as possible, enabling you to find anything that could potentially be a GPS coordinate in documents and communications. Ticking only specific options allows you to make the regex accept only a specific notation so you can use it to validate user input or to extract GPS coordinates from structured files that use a consistent GPS notation.

This example shows how you can use the “GPS coordinates” pattern to match lines that consist of a GPS coordinate. You can find this example as “Pattern: GPS coordinates” in the RegexMagic library.

  1. Click the New Formula button on the top toolbar to clear out all settings on the Samples, Match, and Action panels.
  2. On the Samples panel, paste in one new sample:
    8.08861540422277, 98.29681399263181
    48.85825675817111°, 2.2940979896205285°
    -2.784880729857979,-40.499711632835876
    20.897066728707806, -156.4482494335841°
    48.85825675817111°2.2940979896205285°
    N48.85825675817111°, E2.2940979896205285°
    N48.85825675817111, E2.2940979896205285
    20.897066728707806, 156.4482494335841°W
    48.85825675817111N, 2.2940979896205285E
    N48.85825675817111°E2.2940979896205285°
    N48.85825675817111E2.2940979896205285
    48.85825675817111°N2.2940979896205285°E
    48.85825675817111N2.2940979896205285E
    N 20.897066728707806 W 156.4482494335841
    48.85825675817111 N 2.2940979896205285 E
    08.08861540422277, 098.29681399263181
    48.85825675817111°, 002.2940979896205285°
    -02.784880729857979, -040.499711632835876
    -02.784880729857979, -040.499711632835876°
    N 20.897066728707806 W 156.4482494335841
    48.85825675817111 N 002.2940979896205285 E
    48°51.49541, 2°17.64588
    48°51.49541′2°17.64588′
    48°51.49541′,2°17.64588′
    48°51.49541′, 2°17.64588′
    08°05.31692, 98°17.80884
    08°05.31692, 098°17.80884
    48°51′29.7243, 2°17′38.7528
    48°51′29.7243″2°17′38.7528″
    48°51′29.7243″,2°17′38.7528″
    48°51′29.7243″, 2°17′38.7528″
    48°51'29.7243", 2°17'38.7528"
    48*51'29.7243", 2*17'38.7528"
    48*51´29.7243", 2*17´38.7528"
    48º51’29.7243”, 2º17’38.7528”
    08°05'19.0155, 98°17'48.5304
    08°05'19.0155, 098°17'48.5304
    91.23456789, 1.23456789
    1.23456789, 181.23456789
    1°61.234567, 1°23.45678
    1°23′61.2345, 1°23′45.6789
    48.85825675817111
    2.2940979896205285°
    -2.784880729857979
    -156.4482494335841
    N48.85825675817111
    E2.2940979896205285
    S2.784880729857979
    W40.499711632835876
    48.85825675817111N
    2.2940979896205285E
    2.784880729857979S
    40.499711632835876W
    
  3. On the Match panel, set “begin regex match at” to “start of line”, and set “end regex match at” to “end of line”.
  4. Click the Add First Field button to add field 1.
  5. In the “pattern to match field” drop-down list, select “GPS coordinates”.



  6. Set the “field validation mode” to “average”.
  7. On the Regex panel, select “Python 3.13” as your application, turn off free-spacing, and turn off mode modifiers. Click the Generate button, and you’ll get this regular expression:
    \A([NS-] *)?(?:90|[0-8]?[0-9])(?:\.[0-9]+°?|° *[0-5]?[0-9](?:\.[0-9]+[′']?|[′'] *[0-5]?[0-9]\.[0-9]+[″"]?))(?(1)| *[NS])?, *([EW-] *)?(?:180|1[0-7][0-9]|0?[0-9]{1,2})(?:\.[0-9]+°?|° *[0-5]?[0-9](?:\.[0-9]+[′']?|[′'] *[0-5]?[0-9]\.[0-9]+[″"]?))(?(2)| *[EW])?\Z
    Required options: Case insensitive; Exact spacing.
    Unused options: Dot doesn’t match line breaks; ^$ don’t match at line breaks.
  8. The Samples panel now highlights the GPSs our regex matches:
    8.08861540422277, 98.29681399263181
    48.85825675817111°, 2.2940979896205285°
    -2.784880729857979,-40.499711632835876
    20.897066728707806, -156.4482494335841°
    48.85825675817111°2.2940979896205285°
    N48.85825675817111°, E2.2940979896205285°
    N48.85825675817111, E2.2940979896205285
    20.897066728707806, 156.4482494335841°W
    48.85825675817111N, 2.2940979896205285E
    N48.85825675817111°E2.2940979896205285°
    N48.85825675817111E2.2940979896205285
    48.85825675817111°N2.2940979896205285°E
    48.85825675817111N2.2940979896205285E
    N 20.897066728707806 W 156.4482494335841
    48.85825675817111 N 2.2940979896205285 E
    08.08861540422277, 098.29681399263181
    48.85825675817111°, 002.2940979896205285°
    -02.784880729857979, -040.499711632835876
    -02.784880729857979, -040.499711632835876°
    N 20.897066728707806 W 156.4482494335841
    48.85825675817111 N 002.2940979896205285 E
    48°51.49541, 2°17.64588
    48°51.49541′2°17.64588′
    48°51.49541′,2°17.64588′
    48°51.49541′, 2°17.64588′
    08°05.31692, 98°17.80884
    08°05.31692, 098°17.80884
    48°51′29.7243, 2°17′38.7528
    48°51′29.7243″2°17′38.7528″
    48°51′29.7243″,2°17′38.7528″
    48°51′29.7243″, 2°17′38.7528″
    48°51'29.7243", 2°17'38.7528"
    48*51'29.7243", 2*17'38.7528"
    48*51´29.7243", 2*17´38.7528"
    48º51’29.7243”, 2º17’38.7528”
    08°05'19.0155, 98°17'48.5304
    08°05'19.0155, 098°17'48.5304
    91.23456789, 1.23456789
    1.23456789, 181.23456789
    1°61.234567, 1°23.45678
    1°23′61.2345, 1°23′45.6789
    48.85825675817111
    2.2940979896205285°
    -2.784880729857979
    -156.4482494335841
    N48.85825675817111
    E2.2940979896205285
    S2.784880729857979
    W40.499711632835876
    48.85825675817111N
    2.2940979896205285E
    2.784880729857979S
    40.499711632835876W
    

The generated regex does not match all the lines in the sample text. The sample text contains every notation that is supported by RegexMagic’s pattern for GPS coordinates. But the example does not enable all the options. Some options are mutually exclusive. In particular, you must either require a delimiter between latitude and longitude, or require N/S/E/W compass indicators, or require trailing symbol in order to ensure that there is something to separate the last digit of the latitude from the first digit of the longitude.

The generated regex always allows all combinations of the options that you turn on. If you enable both “decimal degrees” and “degrees minutes seconds” and also enable the minus sign and trailing compass indicators, then the generated regex will match decimal degrees specified with signs or with compass indicators and also DMS coordinates specified with signs or compass indicators. If you want to allow the minus sign only for decimal degrees and the compass indicators only for DMS coordinates then you need to use multiple fields. Add one field that is an alternation field. Inside that field, add two fields that use the “GPS coordinates” pattern. Enable “decimal degrees” and the minus sign for the first field, and “degrees minutes seconds” and compass indicators for the second field.

Reference