如何让我的preg_match显示第一个实例?

时间:2015-04-06 22:41:20

标签: 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,但它返回了相同的结果。

我一直在寻找我的问题的答案,但找不到与之相关的答案。

提前谢谢。

1 个答案:

答案 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];

样本: http://ideone.com/36DRLa

Mastering Quantifiers