Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a regular expression that I need to match which searches for a Latitude Longitude point in a couple of different formats. The problem with this is that it will match partial coordinates out of numbers that shouldn't work as coordinates.

Regex:
([1-8]?[0-9]\.[\d]{1,10}|90\.0+|0\.[\d]{1,10})\s?°?\s?[NS]?\s?\,\s*(([1][8][0]\.0+)|(([0-9]|[1-9][0-9]|1[0-7][0-9]|[0])\.[\d]{1,10}))\s?°?\s?[EW]?


For Example, the above regex will match 90.0000, 180.0000 out of the string "190.0000, 180.0000" which isn't a valid coordinate. The way I've come up with to combat this is placing the Regex inside non-digit character matches:
[^\d]...regex...[^\d]
which works like a charm.

The problem with this is now the digits are included in the match and when I am searching for multiple matches it starts skipping coordinates that only have one white space in between them (like a new line).

So when searching through the string:
"90.0000°, 180.0000°
78.4566°, 177.4444°
12.12345N, 80.676667E"

it will skip 78.4566°, 177.4444°.

My Question is:
Is there a way to match the coordinate (in between two [^\d] but don't include [^\d] in the match so the regex search will begin testing the next match at the last [^\d] in the last match?


-Kyle

This is for a VB.Net 3.5+ Regex
Posted

1 solution

Try this instead of enclosing your exp with [^\d]...regex...[^\d]


(?<!\d)([1-8]?[0-9]\.[\d]{1,10}|90\.0+|0\.[\d]{1,10})\s?°?\s?[NS]?\s?\,\s*(([1][8][0]\.0+)|(([0-9]|[1-9][0-9]|1[0-7][0-9]|[0])\.[\d]{1,10}))\s?°?\s?[EW]?

I've put a 'negative look behind' zero length assertion at the begining
(?<!\d)


For more info on zero-length assertions see msdn or
http://www.regular-expressions.info/lookaround.html[^]
 
Share this answer
 
v2
Comments
Kyle A.B. 30-Oct-13 9:08am    
Excellent, this works exactly. Thanks for the help. The webpage you sent explained the rest of my question as well, my final will be something like this..
Negative Lookbehind followed by the Positive Lookbehind.
(?<!\d)...(?<=\d)

-Kyle

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900