I want to extract a content inside some function such as SUM, AVG, COUNT of a SQL query expression in PHP. For example SUM(A+B) -> A+B or SUM(SUM(A)) -> SUM(A) and A.
I have used a function preg_match_all with a regular expression as follows:
#(SUM|sum|AVG|avg|MIN|min|MAX|max|COUNT|count)\((.*?)\)#
It works very well with a single function such as SUM(A), but not for a multiple function case SUM(SUM(A)). The result of SUM(SUM(A)) is SUM(A. It looks like preg_match_all only recognizes the first ) and skip others next ). 
Is there any way or any function help me to extract the content of the function in multiple function case?
 
    