0

私は正規表現の初心者です。助けてください。以下の文字列は、1 つのドキュメントに含まれています。

not_unique\">海底 20,000 マイル

番号を抽出する必要があります。シーケンス「not_unique」は一意ではなく、このサンプルが来る前にドキュメント全体で数回発生する可能性があります。「miles under sea」の部分はドキュメント固有のものであり、終了区切り文字として使用できます。

PHPで次のようなことを試しましたが、うまくいきませんでした:

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

助けてください!

4

3 に答える 3

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

?>
于 2010-11-15T09:21:03.097 に答える
0

/ユニークではない\">\ s *([0123456789、] +)\s*海中のマイル/

それをする必要があります。

于 2010-11-15T09:17:34.290 に答える
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 を末尾の文字列の正確な長さに置き換えます。

于 2010-11-15T09:42:57.663 に答える