使用正则表达式查找最后一次出现

时间:2010-11-15 09:01:05

标签: php regex last-occurrence

我是一名正念新手,请帮助我。下面的字符串出现在一个文档中:

  

not_unique \“>海下20,000英里

我需要提取数字。序列“not_unique”不是唯一的,并且在此样本到来之前可能会在整个文档中多次出现。 “海里英里”部分对于文档是唯一的,可以用作结束分隔符。

我在PHP中尝试过类似的东西,但它对我不起作用:

if (preg_match('/(?=.*?miles under sea)(?!.+?not_unique)not_unique/', $document, $regs)) {...}

请帮忙!

3 个答案:

答案 0 :(得分:2)

这样的事情怎么样?

<?php

$document = "blah blah blah sjhsdijf  not_unique\">20,000 miles under sea</a> jkdjksds  sdsjdlksdsd k skdjsld sd";

//the made optional, also account for 'leagues' instead of miles

preg_match("/([0-9,]{1,6})\s?(miles|leagues)\sunder(\sthe)?\ssea/i", $document, $matches);

print_r($matches);

?>

答案 1 :(得分:0)

/ not unique \“&gt; \ s *([0123456789,] +)\ s *海底英里/

应该这样做。

答案 2 :(得分:0)

这应该可以解决问题:

preg_match_all('/[1234567890\,]+ miles under sea/i', 'not_unique\">20,000 miles under sea', $result); //find all occurances of the pattern
$tempval=$result[sizeof($result)-1]; //get the last one
$endresult=substr($tempval,0,strlen($tempval)-16); //get the string without the length of the ending string

如果需要 - 将16替换为结束字符串的确切长度。