代码之家  ›  专栏  ›  技术社区  ›  alex

为什么我的简单正则表达式不起作用?

  •  0
  • alex  · 技术社区  · 15 年前

    我有一个步骤列表,它们正在 echo 'd进入视图 li A中的项目 ol .

    所以,我需要删除前导数字 ol { list-style: decimal } .

    下面是一个示例数组成员

    1. 混合1汤匙油…

    我试过这个雷鬼

    /^\d+\.\s*/
    

    但是,它没有移除 1. 在上面的例子中

    但是 ,当我拆下线锚时( ^ ),它有效。

    这是完整的代码。

    foreach($method as &$step) {
    
        $step = trim(preg_replace('/^\d+\.\s*/', '', $step));
        var_dump($step); // This is what I quoted above
    }
    

    我做错什么了?

    谢谢!

    更新

    抱歉,伙计们,这是 var_dump() 其中一行……

    string(43) "4. Remove from heat and cover to keep warm."
    

    在我看来,这个数字之前没有任何东西。

    2 回复  |  直到 15 年前
        1
  •  3
  •   Luke Schafer    15 年前

    /^\s*\d+\.\s*/
    
        2
  •  1
  •   alex    15 年前

    ^ m

    <?php
    $s = <<<EOS
      1. Combine 1 tbs oil...
      2. Hello World
      3. Ok then!
    EOS;
    echo trim(preg_replace('/^\s*\d+\.\s*/m', '', $s));