Reputation: 13
I have the following regex pattern:
^POLYGON\s*\(\((\-?\d+(\.\d+)?)\s*(\-?\d+(\.\d+)?)\)\)$
I am trying to match the string pattern POLYGON ((77.40970726940513 12.978147980713118,77.37585259873805 13.273612877088299,77.80253462545315 13.185991361443607,77.83326404994843 12.874112460078642,77.60553384375903 12.753157552741165,77.47003961302485 12.860758717988348,77.40044069330365 12.989005488377385,77.40970726940513 12.978147980713118)). But the regex pattern which I have used above only satisfies the string with values POLYGON ((130.55809472656256 111.333)) .
Need a help on regex expression to match the string like above with multiple lat long values with comma separated groups.
Upvotes: 1
Views: 57
Reputation: 163352
You can optionally repeat the second part of the pattern and match either a comma or a whitespace char to match the whole string.
^POLYGON\s*\(\(\-?\d+(?:\.\d+)?(?:[,\s]\-?\d+(?:\.\d+)?)*\)\)$
If they should come in pairs separated by a whitespace char and after lat long a comma, you can optionally repeat the pairs:
^POLYGON\s*\(\(\-?\d+(?:\.\d+)?\s\-?\d+(?:\.\d+)?(?:,-?\d+(?:\.\d+)?(?:\s\-?\d+\.\d+)?)*\)\)$
Upvotes: 1