Regular expression to extract lat lon from string

3993
4
03-15-2011 07:26 AM
CarmenDurham
Occasional Contributor II
Hello all,

This has to be easy, but I cannot seem to form the expression correctly.  How can I extract the actual longitude value from the following web query (is a text string at the point where I am wanting to extract)?

Vehicle ID:truck1 lat:34.833730000000003 lon:-82.352369999999993 dir:90.0 spd:0.0 lck:6 time:151842 date:03142011 trig:2 eof<br>

I can match "lon:" using
myString.match(/(lon:)/g)
but I cannot seem to get the portion associated with the value.  I need -82.352369999999993 or lon:-82.352369999999993

The number of digits for the longitude is not always the same, but it is always followed by a space then dir:


Thanks in advance.

Carmen
0 Kudos
4 Replies
AlessioDi_Lorenzo
New Contributor
Try

Longitude
^-{0,1}((180|180.[0]{1,20}|[0-9]|([0-9][0-9])|([1][0-7][0-9]))|(179|[0-9]|([0-9][0-9])|([1][0-7][0-9]))[.]{1}[0-9]{1,20}){1}$


Latitude
^-{0,1}((90|90.[0]{1,20}|[0-9]|[1-8][0-9])|(89|[0-9]|[1-8][0-9])[.]{1}[0-9]{1,20}){1}$


I use these regexp to validate coordinate couples inserted by users in a web page. You could try them to extract lon and lat from your string.
0 Kudos
CarmenDurham
Occasional Contributor II
Thank you for replying.  That helped some.  I had to remove the "^" and "$" for it to test true.  However, I am not getting all of the number.  Here is the small bit of code:
var shortString = "Vehicle ID:truck1 lat:34.833730000000003 lon:-82.352369999999993 dir:90.0 spd:0.0 lck:6 time:151842 date:03142011 trig:2 eof<br>";
var myArray = shortString.match(/lat:-{0,1}((90|90.[0]{1,20}|[0-9]|[1-8][0-9])|(89|[0-9]|[1-8][0-9])[.]{1}[0-9]{1,20}){1}/g);
alert(myArray[0]);


All I get returned is "lat:3"  not the desired "lat:34.833730000000003"
I primarily work with Flex, so forgive me if I missed something obvious with the javascript.

Thanks,
Carmen
0 Kudos
HemingZhu
Occasional Contributor III
Hello all,

This has to be easy, but I cannot seem to form the expression correctly.  How can I extract the actual longitude value from the following web query (is a text string at the point where I am wanting to extract)?

Vehicle ID:truck1 lat:34.833730000000003 lon:-82.352369999999993 dir:90.0 spd:0.0 lck:6 time:151842 date:03142011 trig:2 eof<br>

I can match "lon:" using
myString.match(/(lon:)/g)
but I cannot seem to get the portion associated with the value.  I need -82.352369999999993 or lon:-82.352369999999993

The number of digits for the longitude is not always the same, but it is always followed by a space then dir:


Thanks in advance.

Carmen


try this:
function getlon(myString) {
    var strArray = myString.split(" ");
    for (var i = 0; i < strArray.length; i++) {
        if (strArray.indexOf("lon:") > -1)
            alert(strArray.split(":")[1]);
    }
}
0 Kudos
CarmenDurham
Occasional Contributor II
Heming,

Thank you!  This worked perfectly and also makes more sense to me than using the RegExp.

Carmen
0 Kudos