简体   繁体   English

正则表达式匹配[和]

[英]regular expression to match between [ and ]

I have a string like 我有一个字符串

[gallery GALLERYNAME] [gallery GALLERYNAME]

the GALLERYNAME indicates a name of a gallery GALLERYNAME表示图库的名称

What would a regular expression looks like to match this string? 正则表达式与此字符串匹配的内容是什么?

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

As an alternate: 作为替代:

<?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
?>

要匹配“GALLERYNAME”:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM