简体   繁体   English

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

[英]Find the last occurrence using Regular Expressions

I'm a regex newbie, please help me out. 我是一个正念新手,请帮帮我。 The string below occurs in one document: 下面的字符串出现在一个文档中:

not_unique\\">20,000 miles under sea not_unique \\“>水下20,000英里

I need to extract the number. 我需要提取数字。 The sequence "not_unique" is not unique and may occur in the whole document several times before this sample comes. 序列“not_unique”不是唯一的,并且在此样本到来之前可能会在整个文档中多次出现。 The part "miles under sea" is unique for the document, can be used as ending delimiter. 该文档中“海里的英里”部分是唯一的,可以用作结尾定界符。

I tried something like this in PHP, but it didn't work for me: 我在PHP中尝试过类似的东西,但它对我不起作用:

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

Please help! 请帮忙!

How about something like this? 这样的事怎么样?

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

?>

/ not unique\\">\\s*([0123456789,]+)\\s*miles under the sea / /不是唯一\\“> \\ s *([0123456789,] +)\\ s *海底英里/

should do it. 应该这样做。

This should do the trick: 这应该做的伎俩:

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

If needed - replace 16 with the exact length of the ending string. 如果需要-将16替换为结尾字符串的确切长度。

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

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