php - Match expression between paretheses with nested paretheses -


as long there no nested parenthesis, easy match text between parentheses using regular expressions. use

preg_match('#\(([^)]*)\)#', $subject, $matches); 

and whole expression $matches[0] , part between parentheses $matches[1].

but if have $subject = 'test (with (nested) parenthesis)'; return (of course) 'with (nested' instead of 'with (nested) parenthesis'. how need modify regular expression expected result ?

you can make use of recursive regex:

\(((?:[^()]+|\((?1)\))+)\) 

regex101 demo

(?1) matches first capture group.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -