【发布时间】:2013-04-06 19:55:27
【问题描述】:
我必须从 MYSQL SELECT 中提取列名,我希望使用正则表达式来做到这一点。
这是一个普通的SELECT,类似于:SELECT column1, column2 ... FROM table
我必须涵盖所有情况,我们没有别名,前面有或没有表格,有或没有引用字符:
SELECT column, column as foo, table.column, table.column as foo,
`column`, `column` as foo, `table`.`column`, `table`.`column` as foo
.....
目前我已经能够计算出这个正则表达式:#\w+(\sas)?#i,但这与前缀列相比并不好。
有什么帮助吗?
顺便问一下,Regex 擅长这个任务吗?
编辑
感谢您的回答!
您发布的模式对整个查询都有效,实际上我已经在处理每一列:
$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
}
如上面评论中所述,我总是需要字段名称,而不是别名。 很抱歉之前没有指出!
【问题讨论】:
-
is Regex good at this task这完全取决于您用于编程的语言!指定您使用的语言/工具,以便我们为您提供帮助