I have a string of the following form
$string = "This is {test} for [a]{test2} for {test3}.";
I want to get all curly brackets that are not prefixed by square brackets. Thus, in the above string I would like to get {test}
and {test3}
but not [a]{test2}
.
I found in the answer https://stackoverflow.com/a/977294/2311074 that this might be possible with negative lookahead. So I tried
$regex = '/(?:(?!\[[^\}]+\])\{[^\}]+\})/';
echo preg_match_all($regex, $string, $matches) . '<br>';
print_r($matches);
but this still gives me all three curly brackets.
3
Array ( [0] => Array ( [0] => {test} [1] => {test2} [2] => {test3} ) )
Why is this not working?