3

I draw a line on a google map than I create a string like:

(-25.368819800291383,130.55809472656256)(-25.507716386730266,131.05797265625006) (-24.89637614190406,131.49193261718756)(-24.582068950590272,130.31090234375006)

the first value is the latitude, the second the longitude. I assign a hidden field value tothis string so I can acces it on the server.

How can I retrieve the latitude and longitude numbers?

I was trying

 Match match = Regex.Match(points, @"(^(0|(-(((0|[1-9]\d*)\.\d+)|([1-9]\d*))))$,^(0|(-(((0|[1-9]\d*)\.\d+)|([1-9]\d*))))$)*", RegexOptions.IgnoreCase);
Shai
  • 25,159
  • 9
  • 44
  • 67
Ryan
  • 5,456
  • 25
  • 71
  • 129
  • May be the same as http://stackoverflow.com/questions/3518504/regular-expression-for-matching-latitude-longitude-coordinates – Likurg May 21 '12 at 13:51
  • **How can I retrieve the latitude and longitude numbers?** - You don't need a regular expression to do this. I would argue it would be faster just to use `String.Split` – Security Hound May 21 '12 at 13:52
  • @Ramhound. Some string manipulation sounds workable as well. But after the split you should still do some more parsing right. – buckley May 21 '12 at 14:18

4 Answers4

2

Use this: ^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$ (taken from here). One of the issues with your regex is that you are using ^ and $ twice. These denote the start and end of the string thus in your case your regex will never work.

The regex above should extract the digits and make them available through the use of groups.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
1

Give this a try

\((?<lat>[-\d.]+),(?<long>[-\d.]+)\)

I like to use named groups here. To get only the latitudes use

    Regex regexObj = new Regex(@"\((?<lat>[-\d.]+),(?<long>[-\d.]+)\)");
    Match matchResult = regexObj.Match(subjectString);
    while (matchResult.Success) {
        Console.WriteLine(matchResult.Groups["lat"].Value));
        matchResult = matchResult.NextMatch();
buckley
  • 13,690
  • 3
  • 53
  • 61
0

Try this:

Match match = Regex.Match(points, @"(-?\d+\.\d+)+", RegexOptions.IgnoreCase);

For your input, it will yield 8 results, each result = Longitude OR Latitude

Match match = Regex.Match(points, @"((-?\d+\.\d+)+,?){2}", RegexOptions.IgnoreCase);

Will yield 4 results, each result is a pair of the form Latitude, Longitude

Shai
  • 25,159
  • 9
  • 44
  • 67
0

Try this:

import re
p = re.compile('([+-]*\d*[\.]*\d*), ([+-]*\d*[\.]*\d*)')
t = int(input())
for i in range(t):
    try:
        z = str(input())
        z = z[1:len(z)-1]
        zz = re.findall(p, z)
        zz = zz[0]
        x = zz[0]
        y = zz[1]
        if(x[0] == '+' or x[0] == '-'):
            x = x[1:]
        if(y[0] == '+' or y[0] == '-'):
            y = y[1:]
        if(x[len(x)-1] == '.' or y[len(y)-1] == '.'):
            print("Invalid")
            continue
        if((len(x) > 1 and x[0] == '0' and x[1] != '.') or (len(y) > 1 and y[0] == '0' and y[1] != '.')):
            print("Invalid")
            continue
        x = float(x)
        y = float(y)
        if(x >= -90.0 and x <= 90.0 and y >= -180.0 and y <= 180.0):
            print("Valid")
        else:
            print("Invalid")
    except:
        print("Invalid")
Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55