I want to parse SQL statements with extra notations. I am trying to use the recursive
notation to match nested parentheses. The following example reduces the problem to
the simplest form:
function doMatch($re, $string)
if (preg_match($re, $string, $matches))
for ($i=0; $i<count($matches); $i++)
", $i, $matches[$i]);
printf("No match (boo hoo)\
$string = "<click:blah,blah> (SELECT GROUP_CONCAT(s.fname) FROM Scouts) AS bogus";
$reThatWorks = "/\\((?:([^()]+)|(?R))*\\)/";
$reThatFails = "/ \\((?:([^()]+)|(?R))*\\)/";
This is the output:
0: (SELECT GROUP_CONCAT(s.fname) FROM Scouts)
1: FROM Scouts
using "/ \\((?:([^()]+)|(?R))*\\)/":
No match (boo hoo)
The only difference between the regexes is the failing regex tries to match a space in
front of the nested parentheses part. If fails with a space after as well.
Ultimately, I'd like to match the contents of the <...>, content up to optional "AS alias",
and the alias if used. For now, I'd be happy if I could understand why the space causes
failure, and a workaround, if possible.
Thanks in advance (if anyone takes this on)
PS. I know I could do this by iterating through each character, but using regexes would
be more flexible.
or after the regex for the nested parentheses prevents a match.