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)\))+)\)
(?1)
matches first capture group.
Comments
Post a Comment