正则表达式匹配[和]

时间:2010-10-11 09:18:23

标签: php regex string

我有一个像

这样的字符串
[gallery GALLERYNAME] [gallery GALLERYNAME]

GALLERYNAME表示图库的名称

正则表达式与此字符串匹配的内容是什么?

3 个答案:

答案 0 :(得分:4)

\[gallery ([^\]]+)\]

答案 1 :(得分:4)

<?php
    preg_match( "@\[gallery ([^\]]+)\]@", "foo [gallery GALLERYNAME] bar", $match );
    var_dump( $match );
    // $match[ 1 ] should contain "GALLERYNAME"
?>

作为替补:

<?php
    preg_match_all( "@\[gallery ([^\]]+)\]@m", "
        foo [gallery GALLERYNAME1] bar
        foo [gallery GALLERYNAME2] bar
        foo [gallery GALLERYNAME3] bar
        ", $match );
    var_dump( $match );
    // $match[ 1 ] should contain an array containing the desired matches
?>

答案 2 :(得分:1)

匹配“GALLERYNAME”:

^\[gallery\s([A-Z]+)\]$