Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8910963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:49:37+00:00 2026-06-15T03:49:37+00:00

I have a regular expression that I want to match a latitude/longitude pair in

  • 0

I have a regular expression that I want to match a latitude/longitude pair in a variety of fashions, e.g.

123 34 42
-123* 34' 42"
123* 34' 42"
+123* 34' 42"
45* 12' 22"N
45 12' 22"S
90:00:00.0N

I want to be able to match these in a pair such that

90:00:00.0N 180:00:00.0E is a latitude/longitude pair.

or

45* 12' 22"N 46* 12' 22"E is a latitude/longitude pair (1 degree by 1 degree cell).

or

123* 34' 42" 124* 34' 42" is a latitude/longitude pair

etc

Using the below regular expression, when I type in 123, it matches. I suppose this is true since 123 00 00 is a valid coordinate. However, I want to use this regular expression to match pairs in the same format above

   "([-|\\+]?\\d{1,3}[d|D|\u00B0|\\s](\\s*\\d{1,2}['|\u2019|\\s])?"
 + "(\\s*\\d{1,2}[\"|\u201d|\\s])?\\s*([N|n|S|s|E|e|W|w])?\\s?)"

I am using Java.

* denotes a degree.

What am I doing wrong in my regular expression?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:49:38+00:00Added an answer on June 15, 2026 at 3:49 am

    Well, for one thing, you’re filling your character sets with a bunch of unnecessary pipe characters – alternation is implied in a [] pair. Additional cleanup: + doesn’t need to be escaped in a character class. Your regular expression seems to be addressing a bigger problem statement than you gave us – you make no mention of d or D as matchable character. And you’ve made pretty much the entire back half of your RegEx optional. Going off of what I think your original problem statement is, I built the following regular expression:

    ^\s*([+-]?\d{1,3}\*?\s+\d{1,2}'?\s+\d{1,2}"?[NSEW]?|\d{1,3}(:\d{2}){2}\.\d[NSEW]\s*){1,2}$
    

    It’s a bit of a doozy, but I’ll break it down for you, or anyone who happens across this in the future (Hello, future!).

    ^
    

    Start of string, simple.

    \s*
    

    Any amount of whitespace – even none.

    ( 
    

    Denotes the beginning of a group – we’ll get back to that.

    [-+]?
    

    An optional sign

    \d{1,3}
    

    1 to three digits

    \*?
    

    An optional Asterisk – the escape here is key for an asterisk, but if you want to replace this with the unicode codepoint for an actual degree, you won’t need it.

    \s+
    

    At least one character of whitespace

    \d{1,2}
    

    1 or two digits.

    '?
    

    Optional apostrophe

    \s+\d{1,2}+
    

    You’ve seen these before, but there’s a new curveball – there’s a plus after the {1,2} quantifier! This makes it a possessive quantifier, meaning that the matcher won’t give up its matches for this group to make another one possible. This is almost exclusively here to prevent 1 1 11 1 1 from matching, but can be used to increase speed anywhere you’re 100% sure you don’t need to be able to backtrack.

    "?
    

    Optional double quote. You’ll have to escape this in Java.

    [NSEW]?
    

    An optional cardinal direction, designated by letter

    |
    

    OR – you can match everything in the group before this, or everything in the group after this.

    \d{1,3}
    

    Old news.

    (:\d{2})
    

    A colon, followed by two characters…

    {2}
    

    twice!

    \.\d
    

    Decimal point, followed by a single digit.

    [NSEW]
    

    Same as before, but this time it’s mandatory.

    \s*)
    

    Some space, and finally the end of the group. Now, the first group has matched an entire longitude/latitude denotation, with an arbitrary amount of space at the end. Followed closely by:

    {1,2}
    

    Do that one, or two times – to match a single or a pair, then finally:

    $
    

    The end of the string.

    This isn’t perfect, but it’s pretty close, and I think it answers the original problem statement. Plus, I feel my explanation has demystified it enough that you can edit it to further suit your needs. The one thing it doesn’t (and won’t) do, is enforce that the first coordinate matches the second in style. That’s just too much to ask of Regular Expressions.

    Doubters: Here it is in action. Please, enjoy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use regular expression to find strings in a file that have
I have that text : <a href=/extend/themes/bizway>BizWay</a> And i want to use regular expression
I want to have the regular expression that makes sure the beginning of the
Assuming I have this regular expression: '#artists/(.*)/#' and I want to match this string:
I want to write a regular expression that will match the following string a
I have the following regular expression #^en/cities/(.*?)/$# and I want it to match anything,
I have a series of characters that I want to match with a regular
I'm trying to construct a regular expression that would match a pattern as such:
I have this Regular Expression that matches the following strings: <!-- 09-02-2009 ---> <!--
How can I have a regular expression that tests for spaces or tabs, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.