1

I am receiving an input like this in my program

lat1,long1;lat2,long2;lat3,long3

latitude longitude pairs separated by columns.

No I want to validate this input so that i may not receive wrong data.

I have created a regular expression:

((^(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?));?)

problem is it only validates a single pair not a string of pairs separated by ; as i wish.

If you could help me come up with an expression to validate my data I would be grateful.

khobab
  • 277
  • 5
  • 16
  • 2
    Please post a sample of your data. – Asad Saeeduddin Jan 09 '13 at 15:25
  • @Asad Example of vaild data: (1) 33.44,34.43 (2) 33.44,34.43;43.44,44.43;53.54,34.43 Examples of invalid data: (1) 33.44,37.45,36.46 (2) 33.56,46.77;56 – khobab Jan 09 '13 at 15:45
  • possible duplicate of [Regular expression for matching latitude/longitude coordinates?](http://stackoverflow.com/questions/3518504/regular-expression-for-matching-latitude-longitude-coordinates) – Dave Newton Feb 24 '15 at 17:52

3 Answers3

1

Your expression check only one pair at the start of the string folowed by one column.

You can try this :

^\-?\d+(\.\d+)?,\-?\d+(\.\d+)?(;\-?\d+(\.\d+)?,\-?\d+(\.\d+)?)*$

Thats your regex (without some brackets inutiles) folowed by 0 or more times a column and a pair.

A test with your 4 examples.

Pilou
  • 1,398
  • 13
  • 24
0

How about something like this?

(-?[\d\.]+,-?[\d\.]+)+;?+

I tried it on Rubular.com (permalink), with the test string as following:

-10,10;10.5,-10.337

Result?

Match 1
1.  -10,10
Match 2
1.  10.5,-10.337
karllindmark
  • 6,031
  • 1
  • 26
  • 41
  • But it doesn't seem to work with these data: (1) 33.44,37.45,36.46 (2) 33.56,46.77;56 – khobab Jan 09 '13 at 15:47
  • Define not work - it'll only capture the valid values. @khobab :) – karllindmark Jan 09 '13 at 16:00
  • Means when I check ita against above two values. It verifies them as a match where they are not. BTW the answer above had all what i was looking for ;), thanx for your concern. – khobab Jan 14 '13 at 11:49
0

I would use the following pattern, which can also accept values like .2 along with 0.2.

^-?(\d+(\.\d+)?|\.\d+),-?(\d+(\.\d+)?|\.\d+)(;-?(\d+(\.\d+)?|\.\d+),-?(\d+(\.\d+)?|\.\d+)){2}$

Here's the breakdown.

^                 # beginning of string
    -?                # optional minus sign
    (                 # either
        \d+(\.\d+)?   # an integer with an optional decimal part
        |             # or
        \.\d+         # a decimal number
    )
    ,                 # a comma
    -?                # optional minus sign
    (                 # either
        \d+(\.\d+)?   # an integer and an optional decimal part
        |             # or
        \.\d+         # a decimal number
    )
    (
        ;                 # a semicolon
        -?                # optional minus sign
        (                 # either
            \d+(\.\d+)?   # an integer and an optional decimal part
            |             # or
            \.\d+         # a decimal number
        )
        ,                 # a comma
        -?                # optional minus sign
        (                 # either
            \d+(\.\d+)?   # an integer and an optional decimal part
            |             # or
            \.\d+         # a decimal number
        )
    ){2}              # with 2 repetitions (of the pattern)
$                 # end of string
inhan
  • 7,394
  • 2
  • 24
  • 35