Regex replace word when not enclosed in brackets

后端 未结 2 806
臣服心动
臣服心动 2020-12-22 08:20

I\'m trying to create a regular expression where it replaces words which are not enclosed by brackets.

Here is what I currently have:

$this->parse         


        
2条回答
  •  死守一世寂寞
    2020-12-22 09:14

    You may match any substring inside parentheses with \[[^][]*] pattern, and then use (*SKIP)(*FAIL) PCRE verbs to drop the match, and only match your pattern in any other context:

    \[[^][]*](*SKIP)(*FAIL)|your_pattern_here
    

    See the regex demo. To skip matches inside paired nested square brackets, use a recusrsion-based regex with a subroutine (note it will have to use a capturing group):

    (?\[(?:[^][]++|(?&skip))*])(*SKIP)(*FAIL)|your_pattern_here
    

    See a regex demo

    Also, since you are building the pattern dynamically, you need to preg_quote the $word along with the delimiter symbol (here, /).

    Your solution is

    $this->parsed = preg_replace(
        '/\[[^][]*\[[^][]*]](*SKIP)(*FAIL)|\b(?:' . preg_quote($word, '/') . ')\b/', 
        '[$0[' . implode(",", array_unique($types)) . ']]',
        $this->parsed);
    

    The \[[^][]*\[[^][]*]] regex will match all those occurrences that have been wrapped with your replacement pattern:

    • \[ - a [
    • [^][]* - 0+ chars other than [ and ]
    • \[ - a [ char
    • [^][]* - 0+ chars other than [ and ]
    • ]] - a ]] substring.

提交回复
热议问题