2015-04-06 30 views
1

我有以下代碼我如何讓我的preg_match顯示第一個實例?

$atk="[2] Skedaddle (20) Shuffle Aipom and all basic Energy cards attached to it into your deck. (Discard all other cards attached to Aipom.) (If you have no Benched Pokemon, this attack does nothing.)"; 

    preg_match('#\[(.*)\] (.*) \((.*)\) (.*)#', $atk2, $matchatk2); 
    $atkcost = $matchatk2[1]; 
    $atkname = $matchatk2[2]; 
    $atkdmg = $matchatk2[3]; 
    $atktext = $matchatk2[4]; 

它它返回以下:

2 
Skedaddle (20) Shuffle Aipom and all basic Energy cards attached to it into your deck. 
Discard all other cards attached to Aipom. 
(If you have no Benched Pokemon, this attack does nothing.) 

我需要它返回:

2 
Skedaddle 
20 
Shuffle Aipom and all basic Energy cards attached to it into your deck. (Discard all other cards attached to Aipom.)(If you have no Benched Pokemon, this attack does nothing.) 

我已經perg_match_all嘗試,但它返回相同的結果。

我已經找遍了所有關於我的問題的答案,並找不到與它有關的答案。

預先感謝您。

+0

如果我的回答幫你,請考慮接受其爲正確答案。韓國社交協會。 –

回答

0

您需要非貪心正則表達式量詞?

像這樣:

$atk="[2] Skedaddle (20) Shuffle Aipom and all basic Energy cards attached to it into your deck. (Discard all other cards attached to Aipom.) (If you have no Benched Pokemon, this attack does nothing.)"; 

    preg_match('#\[(.*?)\] (.*?) \((.*?)\) (.*)#', $atk, $matchatk2); 
    $atkcost = $matchatk2[1]; 
    $atkname = $matchatk2[2]; 
    $atkdmg = $matchatk2[3]; 
    $atktext = $matchatk2[4]; 

DEMO: http://ideone.com/36DRLa

Mastering Quantifiers

+0

就是這樣!爲什麼PHP手冊不明確;我讀過這些,並沒有想到我需要什麼。 –

+0

在使用php之前,我使用這個軟件來檢查我的正則表達式的語法和結果。 http://www.regexbuddy.com/ –

相關問題