I miss a function that returns a substring from a string that matches a pattern. Like that:
string substring(regex_pattern)
For example to extract the location from following string:
"A string containing a location lat=48.010101 lon=7.8020202 and so on..."
Or to extract a certain value from a webpage.
return substring that matches pattern
Moderator: Martin
Re: return substring that matches pattern
You can use the function matches and pass a third parameter with an empty list. The list will be populated by the matching groups:
The first group (group zero) contains the entire string, group 1 the text matched within the first group of parentheses.
Please use single quotes for the string literals that contain regex-curly braces, otherwise Automagic thinks that this is an inline expression and replaces it with the value of the inline expression (4 in the above example).
Code: Select all
value = "my code is 1234";
list = newList();
result = matches(value, '.*(\\d{4}).*', list);
log(result);// true
log(list);// [my code is 1234, 1234]
Please use single quotes for the string literals that contain regex-curly braces, otherwise Automagic thinks that this is an inline expression and replaces it with the value of the inline expression (4 in the above example).
Re: return substring that matches pattern
Thanks. But there seems to be a little bug. Not all quantifiers are greedy.
Example:
It extracts [ Dummy Position 47.87834904 8.3787235 , 7.87834904, 8.3787235] instead of [ Dummy Position 47.87834904 8.3787235 , 47.87834904, 8.3787235].
Edit:
Sorry. Not a bug. The .* grabbed it before! Changed to .*?
Great feature!
This is my solution to get the latitude and the longitude from almost everywhere:
Example:
Code: Select all
value=" Dummy Position 47.87834904 8.3787235 ";
list = newList();
result = matches(value, '.*(\\d+\\.\\d+).*(\\d+\\.\\d+).*', list);
log(result);// true
log(list);
Edit:
Sorry. Not a bug. The .* grabbed it before! Changed to .*?
Great feature!
This is my solution to get the latitude and the longitude from almost everywhere:
Code: Select all
//example values:
value=" Position: http://osm.org/go/0DLFjbKG?m, http://download.osmand.net/go?lat=49.124855&lon=8.9739796&z=16 ";
//value="http://www.openstreetmap.org/edit?lon=6.474530&lat=44.65746&zoom=15";
//value="49.5748 5.755751";
list = newList();
result = matches(value, '.*?([Llatitudelongitude]*)[:,._ =]*?([0-9]+\\.[0-9]+).*?([Llatitudelongitude]*)[:,._ =]*?([0-9]+\\.[0-9]+).*?', list);
if (log(result))
{
if (getElement(list,1) =="lon")
{
lat=getElement(list, 4);
lon=getElement(list,2);
}
else
{
lat=getElement(list, 2);
lon=getElement(list,4);
}
}