poshanniraula
poshanniraula

Reputation: 743

Regex: find out first coordinate from a string of list of coordianates

I have string

"Polygon((85.04953560330497 27.767605311484253,84.79588729053454 27.769298229948298,84.7943200187415 27.5526903779245,85.0474690397788 27.55101288321816,85.04953560330497 27.767605311484253))"

I would like to get the first coordinate (in this case :85.04953560330497 27.767605311484253). To achieve the longitude value of 85.04953560330497 I executed a regex

\(\d{2}\.\d{5} and then got var str = (85.04953 and later did str.split('('); to finally get only the number.

But the problem is with Latitude (27.5526903779245). What should I do to find out the first latitude. The problem is the number of decimal places are not fixed i.e. Here there are 13 digits after decimal in longitude(85.04953560330497), but could be any number and cannot use digits after space (because there are other numbers too after space like (27.769298229948298).

I am a beginner to regex please help me find the solution.

Upvotes: 2

Views: 436

Answers (8)

kliron
kliron

Reputation: 4663

Assuming you always want to extract the first two coordinates, setting the comma character as a boundary:

var m = s.match(/Polygon\(\((\d+\.\d+)\s(\d+\.\d+),/),
   longitude = m[1],
   latitude = m[2];

Upvotes: 2

user557597
user557597

Reputation:

In your case the space is a separator between a longitude / latitude pair.
If you expect the string to be this constant format, then you only have to
worry about defining a proper decimal number regex.

This is the only regex I know of to validate a positive integer or decimal
number -

 (?:
      \d+ 
      (?: \. \d* )?
   |  
      \. \d+ 
 )

Then just add capture buffers and put it in the format you need.

 #  (\d+(?:\.\d*)?|\.\d+)[ ](\d+(?:\.\d*)?|\.\d+)

 (                             # (1 start), longitude
      \d+ 
      (?: \. \d* )?
   |  
      \. \d+ 
 )                             # (1 end)
 [ ]                           # space
 (                             # (2 start), latitude
      \d+ 
      (?: \. \d* )?
   |  
      \. \d+ 
 )                             # (2 end)

Upvotes: 2

Paul S.
Paul S.

Reputation: 66304

Try the following RegExp

var re = /(-?\d+(?:\.\d+)?)\s+(-?\d+(?:\.\d+)?)/;

var str = "Polygon((85.04953560330497 27.767605311484253,84.79588729053454 27.769298229948298,84.7943200187415 27.5526903779245,85.0474690397788 27.55101288321816,85.04953560330497 27.767605311484253))"
str.match(re); // ["85.04953560330497 27.767605311484253", "85.04953560330497", "27.767605311484253"]
               //   whole match,                            first group,         second group

If you want to find all of them, you could use a g flag, but with the string you have it looks easier to simply trim off Polygon(( and )) then str.split on ,s


(pattern)    Capture group, remembers match for pattern, lets you treat it as one item
(?:pattern)  Non-capture group, forgets match for pattern, lets you treat it as one item
x?           Matching (item) x is optional, greedy
x??          Matching (item) x is optional, non-greedy (not used here)
x+           Match at least one repeat of (item) x, greedy
x+?          Match at least one repeat of (item) x, non-greedy (not used here)
\d           Digit, same as [0-9]
\s           Whitespace, i.e. spaces, tabs, new lines, etc
\.           A literal ".", rather than wildcard (which is what . usually means)
/pattern/ig  pattern is a RegExp literal, i and g are flags
\/           A literal "/", rather than terminating the RegExp literal

Upvotes: 2

Salman Arshad
Salman Arshad

Reputation: 272066

No regex solution:

var string = "Polygon((85.04953560330497 27.767605311484253,84.79588729053454 27.769298229948298,84.7943200187415 27.5526903779245,85.0474690397788 27.55101288321816,85.04953560330497 27.767605311484253))";
var latlng = string.replace("Polygon((", "").replace("))", "").split(",")[0].split(" ");
latlng // ["85.04953560330497", "27.767605311484253"]

Upvotes: 2

m.cekiera
m.cekiera

Reputation: 5395

You can use:

\((\d+\.\d+)\s(\d+\.\d+)

regex DEMO

and get numbers by calling captured groups like:

var str = "Polygon((85.04953560330497 27.767605311484253,84.79588729053454 27.769298229948298,84.7943200187415 27.5526903779245,85.0474690397788 27.55101288321816,85.04953560330497 27.767605311484253))"
var regex = /\((\d+.\d+)\s(\d+.\d+)/;
var match = regex.exec(str);
var a = match[1];
var b = match[2];

jsDEMO

Upvotes: 2

dakab
dakab

Reputation: 5875

You could use the first item of this match:

    str = str.match(/(-?\d{1,2}\.\d+ ?){2}/)[0];
    // === "85.04953560330497 27.767605311484253"

Explanation:

/
    (            // capturing group
        -?       // a minus if present (west/south of 0/0)
        \d{1,2}  // one or two digits
        \.       // definitely a decimal point
        \d+      // at least one digit
        ␣?       // an optional trailing single space
    ){2}         // two singleton float coordinates
/

Upvotes: 2

Brian M. Hunt
Brian M. Hunt

Reputation: 83778

Check out http://www.regular-expressions.info/floatingpoint.html

Depending on how regular the co-ordinates are, it could be as simple as

\([\d\.]+

Upvotes: 2

epascarello
epascarello

Reputation: 207501

Use two groupings

var str = "Polygon((85.04953560330497 27.767605311484253,84.79588729053454 27.769298229948298,84.7943200187415 27.5526903779245,85.0474690397788 27.55101288321816,85.04953560330497 27.767605311484253))"
str.match(/(\d+.\d+)\s(\d+.\d+)/);

Upvotes: 2

Related Questions