Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression to match between [ and ]

Tags:

string

regex

php

I have a string like

[gallery GALLERYNAME] [gallery GALLERYNAME]

the GALLERYNAME indicates a name of a gallery

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

like image 382
dotty Avatar asked Dec 05 '25 20:12

dotty


2 Answers

<?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
?>
like image 79
Salman A Avatar answered Dec 08 '25 11:12

Salman A


\[gallery ([^\]]+)\]
like image 27
reko_t Avatar answered Dec 08 '25 12:12

reko_t



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!