Capture contents of nested brackets using PCRE Regexp in PHP?

  • Thread starter Thread starter BigWILL
  • Start date Start date
B

BigWILL

Guest
I have to convert some functions in an SQL statement from MySQL to SQL Server, but I am stuck capturing the functions and their contents for conversion.

Take the following SQL Statement:

SELECT IF(CHARACTER_LENGTH(`projectDesc`)>200, CONCAT(SUBSTRING(`projectDesc`, 1, 200), "..."), `projectDesc`) AS `desc` FROM `projects`;

I need my regexp to capture the following:

$matches = Array(
[0] => IF(CHARACTER_LENGTH(`projectDesc`)>200, CONCAT(SUBSTRING(`projectDesc`, 1, 200), "..."), `projectDesc`)
[1] => IF
[2] => CHARACTER_LENGTH(`projectDesc`)>200, CONCAT(SUBSTRING(`projectDesc`, 1, 200), "..."), `projectDesc`
)

And then recurse the inner functions, there are 2 options here:

1 - To get PCRE to capture all the nested recursions and process them with my callback, in which case it would be best to process them inside out, i.e. deepest child first, then its parent etc.

2 - To handle the recursion through the callback in PHP, in which case only the outer bracketed items need capturing in each pattern, here the number of brackets will need to be balance in the expression

What I have so far falls over with capturing the contents of the function when if has nested functions, the pattern is:

/([A-Z0-9_]*)\(((?R)|(?>[^()]+))*\)/

Please help!!!!
 
Back
Top