Reputation: 119
In the following snippet pre_match returns the first latitude reading followed by a great many characters then the longitude reading.
What I am after is the final latitude/longitude pair.
How do I limit the space between the two readings to a maximum of say 4 characters?
The .*?
expression allows too many characters.
How might I change this to allow a smaller number?
<?php
$lcline="tuesday, 20th. light airs and clear weather. p.m. started at latitude 24 degrees 12 minutes took several azimuth, which gave the variation 16 degrees 30 minutes west. put the ship's company to three watches. wind variable; course south 21 degrees 30 minutes west; distance 28 miles; latitude 31 degrees 17 minutes, longitude 17 degrees 19 minutes west; at noon, funchall, island of madeira, north 13 degrees east, 76 miles. ";
preg_match('/(latitude\s+\d+\s+degrees\s+\d+\s+minutes.*?longitude\s+\d+\s+degrees\s+\d+\s+minutes)/', $lcline, $results);
echo "results=".$results[0]."<BR><BR>"
?>
Upvotes: 1
Views: 361
Reputation: 474
If u say limit, you can use curly braces
eg: /\s{4}/
this will select all with 4 white space characters consecutively
Upvotes: 0
Reputation: 725
Expression
Function
{n}
Match exactly n times, example:Click to test "\w{2}" equals "\w\w"; Click to test "a{5}" equals "aaaaa"
{m,n}
At least m but no more than n times: Click to test "ba{1,3}" matches "ba","baa","baaa"
{m,}
Match at least n times: Click to test "\w\d{2,}" matches "a12","_456","M12344"...
?
Match 1 or 0 times, equivalent to {0,1}: Click to test "a[cd]?" matches "a","ac","ad".
Match 1 or more times, equivalent to {1,}: Click to test "a+b" matches "ab","aab","aaab"...
Match 0 or more times, equivalent to {0,}: Click to test "\^*b" matches "b","^^^b"...
Reference : http://www.regexlab.com/en/regref.htm
Upvotes: 0
Reputation: 91385
Add .+
at the begining of the regex and get the result from group 1:
preg_match('/.+(latitude\s+\d+\s+degrees\s+\d+\s+minutes.*?longitude\s+\d+\s+degrees\s+\d+\s+minutes)/s', $lcline, $results);
echo "results=",$results[1],"\n";
Output:
results=latitude 31 degrees 17 minutes, longitude 17 degrees 19 minutes
Upvotes: 1