繁体   English   中英

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

[英]Find the last occurrence using Regular Expressions

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

not_unique \\“>水下20,000英里

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

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

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

请帮忙!

这样的事怎么样?

<?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);

?>

/不是唯一\\“> \\ s *([0123456789,] +)\\ s *海底英里/

应该这样做。

这应该做的伎俩:

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替换为结尾字符串的确切长度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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