【问题标题】:preg_match for certain part of the commentspreg_match 某些部分的评论
【发布时间】:2012-11-01 13:39:02
【问题描述】:

我想找到 cmets 的某些部分。以下是一些例子

/*
 * @todo name another-name taskID
 */

/* @todo name another-name taskID */

目前我正在使用以下正则表达式

'/\*[[:space:]]*(?=@todo(.*))/i'

这会返回以下内容:

* @todo name another-name taskID
or
* @todo name another-name taskID */

我怎样才能只得到@todo 和名字,而不是任何星号?

想要的输出

@todo name another-name taskID
@todo name another-name taskID

【问题讨论】:

    标签: php regex preg-match preg-match-all preg-replace-callback


    【解决方案1】:

    试试这个:

    $str=<<<STR
    /*
     * @todo name another-name taskID 1
     */
    
    /* @todo name another-name taskID 2 */
    STR;
    $match=null;
    preg_match_all("/\*[[:space:]]*(@todo[^(\*\/$)]*)/i",$str,$match,PREG_SET_ORDER);
    print_r($match);
    

    回声

    Array
    (
        [0] => Array
            (
                [0] => * @todo name another-name taskID 1
    
                [1] => @todo name another-name taskID 1
    
            )
    
        [1] => Array
            (
                [0] => * @todo name another-name taskID 2 
                [1] => @todo name another-name taskID 2 
            )
    
    )
    

    虽然不是一个完美的解决方案(请注意$match[0][0] 中的行尾)。

    【讨论】:

    • 有没有办法,我可以忽略第一个星号?不删除检查
    • @Basit 这就是我使用捕获的原因。可以看到$match[*][1]都是@todo .....
    猜你喜欢
    • 1970-01-01
    • 2018-10-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    相关资源
    最近更新 更多