[英]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.