preg_match Parenthesis pairs matching

i'm attempting to match the outer round brackets pair in a string...but can't seem to be able to define it correctly.

this is what i need the preg_match_all function to acomplish:

let's say i have a string like this:


weeeeeeee (blabla lala() lalalala () (aaaaa(bkbk))) whaaaaaaaa wuuuu ( damn)

i want it to match the most outer pairs of round brackets...meaning that first match in preg_match_all in the upper case would return:

(blabla lala() lalalala () (aaaaa(bkbk)))

and the second match would be:

( damn)


I hope you can all understand the problem =)

LVL 1
coyoteeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
From the manual on http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

<?php
  $str = 'weeeeeeee (blabla lala( ) lalalala ( ) (aaaaa(bkbk))) whaaaaaaaa wuuuu ( damn)';
  $preg = '#\( ( (?>[^()]+) | (?R) )* \)#x';
  if(preg_match_all($preg, $str, $matches)) {
    $matches = $matches[0];
  } else {
    $matches = array();
  }
  echo '<pre>'.var_export($matches,true).'</pre>';
 
?>

-r-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
coyoteeAuthor Commented:
wow that was a fast answer.thank you :)
coyoteeAuthor Commented:
uf just noticed i forgot smth....
would it be possible to add the words next to the brackets to the matches?

so that it says:

whaaaaaaaa wuuuu ( damn)

and

weeeeeeee (blabla lala( ) lalalala ( ) (aaaaa(bkbk)))

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

RoonaanCommented:
Not sure, you could try the following, but I have no server access at the moment to test it for you:

$preg = '#[^\(\)]*\( ( (?>[^()]+) | (?R) )* \)#x';

-r-
RoonaanCommented:
Ah, i found a test server. It seems to work with the above change. Full code would therefor be:

<?php
  $str = 'weeeeeeee (blabla lala( ) lalalala ( ) (aaaaa(bkbk))) whaaaaaaaa wuuuu ( damn)';
  $preg = '#[^\(\)]*\( ( (?>[^()]+) | (?R) )* \)#x';
  if(preg_match_all($preg, $str, $matches)) {
    $matches = $matches[0];
  } else {
    $matches = array();
  }
  echo '<pre>'.var_export($matches,true).'</pre>';
 
?>

-r-
coyoteeAuthor Commented:
wuhu i had to use 2 preg_matches.you just saved me cpu time =)
thank you again
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.