dqwcdqs358367 2010-02-05 03:56
浏览 147
已采纳

PHP正则表达式 - 重复匹配组

I have a string that may look something like this:

$r = 'Filed under: <a>Group1</a>, <a>Group2</a>';

Here is the regular expression I am using so far:

preg_match_all("/Filed under: (?:<a.*?>([\w|\d|\s]+?)<\/a>)+?/", $r, $matches);

I want the regular expression to inside the () to continue to make matches as designated with the +? at the end. But it just won't do it. ::sigh::

Any ideas. I know there has to be a way to do this in one regular expression instead of breaking it up.

  • 写回答

4条回答 默认 最新

  • duanmu8911 2010-02-05 04:17
    关注

    Try:

    <?php
    
    $r = 'Filed under: <a>Group1</a>, <a>Group2</a>, <a>Group3</a>, <a>Group4</a>';
    
    if(preg_match_all("/<a.*?>([^<]*?)<\/a>/", $r, $matches)) {
        var_dump($matches[1]); 
    }
    
    ?>
    

    output:

    array(4) {
      [0]=>
      string(6) "Group1"
      [1]=>
      string(6) "Group2"
      [2]=>
      string(6) "Group3"
      [3]=>
      string(6) "Group4"
    }
    

    EDIT:

    Since you want to include the string 'Filed under' in the search to uniquely identify the match, you can try this, I'm not sure if it can be done using a single call to preg_match

    // Since you want to match everything after 'Filed under'
    if(preg_match("/Filed under:(.*)$/", $r, $matches)) {
        if(preg_match_all("/<a.*?>([^<]*?)<\/a>/", $matches[1], $matches)) {
            var_dump($matches[1]); 
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据