Axel Axel - 10 months ago 32
PHP Question

How to get everything between brackets but only those, which are not wrapped into brackets themselves

By now i'm using regex:

to get everything inside curly barackets in strings like:
'abc {get me} def {and get me}'
which works fine.

Now i'm looking for a way to exclude brackets, which are surrounded by brackets themselves. The string should be self-explanatory:
'abc {get me {!but not me!}} def {and get {!but not me again!} me} ghi'

The following code should illustrate my needs...

$regex = '/{(.*?)}/';
$string = 'abc {get me {!but not me!}} def {and get {!but not me again!} me} ghi';
preg_match_all( $regex, $string, $matches );
print_r( $matches[1] );


[0] => get me {!but not me!
[1] => and get {!but not me again!

but should be:

[0] => get me {!but not me!}
[1] => and get {!but not me again!} me

Until now i was not able to find a solution via Google and looking forward to get one at SO.


Actually a very similar question was asked and answered on SO. But if you are not familiar with regex it's really hard to change from wrapper (...) to {...}. So you can not simply take the one pattern given by the answer (see link above) and search-replace all '(' & ')' with '{' & '}'. You really have to understand what's going on there.

But if you understand this pattern enough to customize it you probably would be able to write it on your own...


You need a recursive regex with a capturing group that would capture the contents inside the curly braces:



  • { - a literal { (in PHP PCRE, you do not need to escape this character)
  • ( - Start of the capturing group 1
    • (?:[^{}]++|(?R))* - zero or more sequences (see (?:...)*) of 1+ characters other than { and } (see [^{}]++) or the whole pattern ((?R) is a placeholder for the whole pattern, like (?0) subroutine call)
  • ) - end of Group 1
  • } - closing literal }

See the regex demo and the PHP demo:

$re = '~{((?:[^{}]++|(?R))*)}~'; 
$str = "abc {get me {!but not me!}} def {and get {!but not me again!} me} ghi"; 
preg_match_all($re, $str, $matches);