简体   繁体   中英

How to extract values from an output string of map?

I have some strings as an output of a map.

How can I extract last two values which are Latitude and Longitude from the string. As the output strings are:

10337 Lamar Avenue, Overland Park, KS 66207, USA, 38.94048888688807, -94.65758273359376

Mirzapur To Niyana Road, Niyana, Haryana 125044, India, 29.163692827306072, 75.83599163535155

301-349 South Walnut Street, Coffeyville, KS 67337, USA, 37.04092845300189, -95.61538733789064

Now I want to store last two values from the string in two different variable.

for example from first string:

$Latitude = 38.94048888688807 ;
$Longitude = -94.65758273359376 ;

I am poor in regex, so please help me.

Try with Positive Lookbehind and get the matched group from index 1 and 2.

(?<=,\s)(-?\d+\.\d+),\s(-?\d+\.\d+)$

Online demo

Sample code:

$re = "/(?<=,\\s)(-?\\d+\\.\\d+),\\s(-?\\d+\\.\\d+)$/m"; 
$str = ...

preg_match_all($re, $str, $matches);

This is the expression you're looking for:

,\s*(-?\d+\.\d+),\s*(-?\d+\.\d+)$

Capturing group 1 will contain the latitude, capturing group 2 will contain the longitude.

Fiddle available here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM