I have to extract column names from a MYSQL SELECT
and I'd wish to do that using Regex.
It's a plain SELECT
, something like:
SELECT column1, column2 ... FROM table
I have to cover every cases, with our without alias, with or without table in front of it, with or without the quoting char:
SELECT column, column as foo, table.column, table.column as foo,
`column`, `column` as foo, `table`.`column`, `table`.`column` as foo
.....
Currently I've been able to work out this regex: #\w+(\sas)?#i
but it's not good vs prefixed columns.
Any help?
By the way, is Regex good at this task?
EDIT
Thanks for the answers!
The patterns you posted are valid for the whole query, actually I'm already processing every single column:
$fields = Frameworkmethod::getSelectFields($query);
$columns = explode(',' , $fields);
foreach($columns as $column)
{
//do Regex work to "clean up" the single field and get the "standard" one (not the alias)
//`#__tracktime_projects`.`pr_name` AS `project_name` should return pr_name
}
As stated in the comment above, I always need the field name, not the alias one. Sorry for not pointing it out before!