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.
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.
Share Improve this question edited Jul 24, 2015 at 17:34 Vbasic4now 5893 gold badges8 silver badges35 bronze badges asked Jul 24, 2015 at 17:28 poshanniraulaposhanniraula 7431 gold badge7 silver badges14 bronze badges8 Answers
Reset to default 2You 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
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+)/);
Check out http://www.regular-expressions.info/floatingpoint.html
Depending on how regular the co-ordinates are, it could be as simple as
\([\d\.]+
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
/
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"]
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
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)
Assuming you always want to extract the first two coordinates, setting the ma character as a boundary:
var m = s.match(/Polygon\(\((\d+\.\d+)\s(\d+\.\d+),/),
longitude = m[1],
latitude = m[2];