parse coordinates from text

1.2k views Asked by At

I like to extract the coordinates from this text:

Text
492k | 505k auf Punkt
https://www.seite.com/map?ll=30.123456,21.123456
Text
83k | 1,8 Mio auf Punkt
https://www.seite.com/map?ll=11.146724,27.427684
Text
82k | 121k auf Punkt
https://www.seite.com/map?ll=24.142451,36.127474
Text
20k | 65k auf Punkt
https://www.seite.com/map?ll=26.241442,16.323624
Text
11k | 93 auf Punkt
https://www.seite.com/map?ll=47.139682,14.124675

I have tried but it did not work well: https://jsfiddle.net/pnyqrgfz/ The result should be like this:

30.123456,21.123456
11.146724,27.427684
24.142451,36.127474
26.241442,16.323624
47.139682,14.124675
3

There are 3 answers

2
seppsepp On BEST ANSWER

What about this short solution:

var source = 'https://www.seite.com/map?ll=30.123456,21.123456';
var pattern = /[0-9]+\.[0-9]+/g;
source.match(pattern);

Output:

["30.123456", "21.123456"]

RegEx example with the complete and long input:

https://regex101.com/r/5FXAW8/1

2
Ankit Agarwal On

You can do this:

function parse () 
{
 var lines = $("#textarea_input").val().split('\n');
 var coords = [];
 for(var i = 0;i < lines.length;i++)
 {
  //code here using lines[i] which will give you each line
    if(lines[i].indexOf('?ll=') !== -1){
     var lineArr = lines[i].split('?ll='); 
      if(lineArr.length!==0){
        var splitCords = lineArr[1].split(',');
        coords.push(splitCords[0]);
        coords.push(splitCords[1]);
      }
    }
 }

 for(var i = 0;i < coords.length;i++)
 {
  //console.log(coords);
  var newLine = '';
  var comma = ','
 if(i%2 !== 0){
    newLine='\n';
    comma = '';
  }  
  
  $('#textarea_output').val($('#textarea_output').val()+coords[i]+comma+newLine); 
 }
}
parse();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 
 <textarea id="textarea_input" rows="15" cols="55">Text
492k | 505k auf Punkt
https://www.seite.com/map?ll=30.123456,21.123456
Text
83k | 1,8 Mio auf Punkt
https://www.seite.com/map?ll=11.146724,27.427684
Text
82k | 121k auf Punkt
https://www.seite.com/map?ll=24.142451,36.127474
Text
20k | 65k auf Punkt
https://www.seite.com/map?ll=26.241442,16.323624
Text
11k | 93 auf Punkt
https://www.seite.com/map?ll=47.139682,14.124675</textarea>
 <input type = "button" onclick = "parse();"/>
 <textarea id="textarea_output" rows="15" cols="55"></textarea>

Make use of split() function and it works great.

0
marvel308 On

You can use the following

function parse () 
{
 var lines = $("#textarea_input").val().split('\n');
 var coords = [];
 for(var i = 0;i < lines.length;i++)
 {
  //code here using lines[i] which will give you each line
  var m = lines[i].match(/\d{1,2}\.\d+,\d{1,2}\.\d+/);
  console.log(lines[i]);
  console.log('i:' + i + ' - ' + m);
  if (m != null)
  {
   coords.push(m);
   console.log(m);
  }
 }

 for(var i = 0;i < coords.length;i++)
 {
  //console.log(coords);
  $('#textarea_output').val($('#textarea_output').val()+coords[i]+'\n'); 
 }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="textarea_input" rows="15" cols="55">Text
492k | 505k auf Punkt
https://www.seite.com/map?ll=30.123456,21.123456
Text
83k | 1,8 Mio auf Punkt
https://www.seite.com/map?ll=11.146724,27.427684
Text
82k | 121k auf Punkt
https://www.seite.com/map?ll=24.142451,36.127474
Text
20k | 65k auf Punkt
https://www.seite.com/map?ll=26.241442,16.323624
Text
11k | 93 auf Punkt
https://www.seite.com/map?ll=47.139682,14.124675</textarea>
 <input type = "button" onclick = "parse();"/>
 <textarea id="textarea_output" rows="15" cols="55"></textarea>