2015-04-06 77 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/ –

相关问题