doubi7346 2018-07-30 22:29
浏览 12
已采纳

PHP preg_match_all谜语

I'm using PHP version 5.6 and I can't figure out why the regular expression won't match the second row correctly.

 $str = '<tr><td class="DH">Sale Date</td></tr><tr><td class="DD">10-MAR-15</td></tr><tr><td class="DD">18-APR-17</td></tr>';

 preg_match_all('/<tr>.*?class="D.*?<\/tr>/', $str, $matches);
 print_r($matches);

 preg_match_all('/<tr>.*?class="DH.*?<\/tr>/', $str, $matches);
 print_r($matches);

 preg_match_all('/<tr>.*?class="DD.*?<\/tr>/', $str, $matches);
 print_r($matches);

This code outputs:

Array
(
    [0] => Array
        (
            [0] => <tr><td class="DH">Sale Date</td></tr>
            [1] => <tr><td class="DD">10-MAR-15</td></tr>
            [2] => <tr><td class="DD">18-APR-17</td></tr>
        )

)
Array
(
    [0] => Array
        (
            [0] => <tr><td class="DH">Sale Date</td></tr>
        )

)
Array
(
    [0] => Array
        (
            [0] => <tr><td class="DH">Sale Date</td></tr><tr><td class="DD">10-MAR-15</td></tr>
            [1] => <tr><td class="DD">18-APR-17</td></tr>
        )

)

The regex essentially means match all shortest sequences between <tr> and </tr> that contain class="D.

Notice how the first regex matches all 3 rows individually correctly.

The second one does the same but wants the row to contain class="DH which it does correctly.

The third regex is supposed to match the other rows which contain class="DD. For some reason only the first result (corresponding to the second table row) wants to include the previous row.

Even if I add a space between </tr> and <tr> as in </tr> <tr>I'm getting the same result. However, if I insert a line break things work.

Can anyone explain what's going on and how to fix my code?

  • 写回答

1条回答 默认 最新

  • dscbxou1900343 2018-07-30 22:36
    关注
    /<tr>.*?class="DD.*?/
    

    says "find <tr>, then match everything until you find class="DD". So it sees:

    <tr><td class="DH">Sale Date</td></tr><tr><td class="DD">
    

    and matches the first <tr>, then the .* matches <td class="DH">Sale Date</td></tr><tr><td, then it sees class="DH" which matches the next part.

    When you add a line break, .* stops matching, so it makes it work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因