I have searched high and low and am really struggling to find the appropriate REGEX that would help me retrieve what I want. Assume I have the following query string:
SELECT col, col2 AS c2, col3, col * col2 calc FROM...
I want a REGEX that will pull out everything between commas where there is a space or an "AS" that proceeds a column alias. It should also exclude the SELECT and FROM. With the example above, I would want the following matches:
col2 AS c2
col * col2 calc
Essentially, this should remove col and col3 because they don't have aliases. I have tried variations of the following but have been unsuccessful (Note: $colname contains the alias):
/(SELECT|,)([^,]*{$colname}[^,]*)(FROM|,)/
Currently this only returns:
, col2 AS c2,
col2 AS c2
, col2 AS c2,
col2 AS c2
I feel I am close but I need some guidance. Any suggestions are appreciated.