There is such a regular that chooses everything between the brackets with them, I can’t add an exception to skip the bracket with the word table inside. Can anyone help?

/\[[^\]]*\]/ 

    1 answer 1

    you can use

     '~\[(?![^][]*table)[^][]*]~' 

    or, if the word table must be a whole word:

     '~\[(?![^][]*\btable\b)[^][]*]~' 

    See the regular expression demo

    Details

    • \[ - character [
    • (?![^][]*\btable\b) is a negative forward preview block, which, if there is a pattern match, cancels the match:
      • [^][]* - 0 or more characters other than [ and ]
      • \btable\b - the whole word table (if checking for a whole word is optional, delete \b )
    • [^][]* - 0 or more characters other than [ and ]
    • ] - character ] .

    PHP demo :

     $re = '/\[(?![^][]*\btable\b)[^][]*]/'; $str = '[kjkj] jkhjkhjk [table] [wedfwefwef] [bgfghfghf] [fvdfvd][sdfsd] [terms] [5465456] jkhjkhjkh'; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); print_r($matches[0]); // => Array ( [0] => [kjkj] )