开发者

regular expression to match between [ and ]

开发者 https://www.devze.com 2023-01-19 17:22 出处:网络
I have a string like [gallery GALLERYNAME] [gallery GALLERYNAME] the GALLERYNAME indica开发者_如何学JAVAtes a name of a gallery

I have a string like

[gallery GALLERYNAME] [gallery GALLERYNAME]

the GALLERYNAME indica开发者_如何学JAVAtes a name of a gallery

What would a regular expression looks like to match this string?


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


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


To match "GALLERYNAME" :

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号