我正试图用正则表达式分割句子

时间:2012-09-27 15:55:45

标签: php regex

我一直在努力解决这一问题,但我怎么能按照以下顺序可靠地拆分下一行。

  

被动:标记与附近盟友隔离的敌人。活性:   造成70/100/130/160/190(+)物理伤害。如果目标是   隔离后,数量增加到100/145/190/235/280(+)。进化   放大的爪子:增加对孤立敌人的伤害   12/12/12/12/12%的健康状况(最大200/200/200/200/200 vs.   怪物)。增加品味他们的恐惧和Kha'Zix的基本范围   攻击时间为50/50/50/50/50。

Array
(
    [0] =>  Array
        (
            [0] => Passive
            [1] => Marks enemies who are isolated from nearby allies.
        )
    [1] =>  Array
        (
            [0] => Active
            [1] => Deals 70/100/130/160/190 (+) physical damage. If the target is isolated, the amount is increased to 100/145/190/235/280 (+).
        )
    [2] =>  Array
        (
            [0] => Evolved Enlarged Claws   
            [1] => Increases damage to isolated enemies by 12/12/12/12/12% of their missing health (max 200/200/200/200/200 vs. monsters). Increases the range of Taste Their Fear and Kha'Zix's basic attacks by 50/50/50/50/50.
        )
)

我无法超越中期句子。

3 个答案:

答案 0 :(得分:2)

(?:^|\s*)([^.:]+):\s*(.+?\.)(?=[\w\s]+:|$)

名称位于捕获组1中,描述位于捕获组2中

答案 1 :(得分:1)

嗯,这似乎可以解决问题......

preg_match_all('/(?<=^|[.] )([^:]+): (.+?[.])(?=[^.]+:|$)/', $text, $matches, PREG_SET_ORDER);
var_dump($matches);

...如果我理解给定的结构('Term: Description. Term: Description'等)。它实际上是描述中的冒号可能会破坏它;这里的点很好。

这种匹配的结果可以很容易地转换为关联数组:

$spell = array();
foreach ($matches as $match) {
   $spell[$match[1]] = $match[2];
}

答案 2 :(得分:0)

使用.作为父/主数组的分隔符,使用:作为子数组。

function so12625378_explode_to_multiarray( $string )
{
    foreach ( explode( ". ", $your_string ) as $part )
        $result[] = explode( ": ", $part );

    return $result;
}

使用:

 $string = 'Passive: Marks enemies who are isolated from nearby allies. Active: Deals 70/100/130/160/190 (+) physical damage. If the target is isolated, the amount is increased to 100/145/190/235/280 (+).';
 echo '<pre>'.var_export( so12625378_explode_to_multiarray( $string ), true ).'</pre>';