0

xml wikipedia エクスポートから取得した Wikipedia XML を解析しようとしています

あるケースでは、すべての画像パスを抽出する必要があります。生のマークアップは次のようになります。

  [[Bild:nameOfImage.png|image description]]

"Bild" は、"Image"、"File"、または "Datei" の場合もあります。

画像のテキストを抽出するには、この正規表現を使用します。

'|\[\[.*\|.*\]\]|U'

画像の説明に他の '[[ .. ]]' が含まれていない場合、これは正常に機能します。

[[Bild:nameOfImage.png|image Description with a [[new wiki link]] ]]

私の質問は、すべての '[' と ']' 文字をカウントせずに、最初の "[[" と最後の"]]" の間のすべてのテキストを取得するように正規表現を変更するにはどうすればよいかということです。

前もって感謝します

4

1 に答える 1

2

Since you're using PHP, you're probably able to use recursive patterns.
Considering you're not capturing anything:

/\[\[(((?>[^\[\]])|(?R))*)\]\]/U

Note that I haven't tried this regex since I have no way to use PHP.

Edit:

preg_match('/\[\[(?>[^\[\]]|(?R))*\]\]/U', '[[Bild:nameOfImage.png|image Description with a [[new wiki link]] ]]', $array);
var_dump($array);

seems to work.

于 2013-03-28T15:50:17.470 に答える