[英]PHP (regex): Extract column names from SQL-Query
I am struggling with a regular expression in PHP:我正在努力解决 PHP 中的正则表达式:
I have a number of SQL-queries.我有许多 SQL 查询。 Now, I'd like to extract the variable names (column names) within these queries.现在,我想在这些查询中提取变量名(列名)。 It should return an array (preg_match_all() )它应该返回一个数组(preg_match_all())
As the queries are rather simple no perfect solution is needed.由于查询相当简单,因此不需要完美的解决方案。 The only conditions I would like to check are the following:我想检查的唯一条件如下:
I was playing around with the following online regex-tester: https://www.phpliveregex.com/#tab-preg-match-all .我正在使用以下在线正则表达式测试器: https://www.phpliveregex.com/#tab-preg-match-all 。 However, the closest I got was this, as I dont know how to get multiple matches:但是,我得到的最接近的是这个,因为我不知道如何获得多个匹配项:
preg_match_all('/^[where|and|or|\(]* ([a-zA-Z0-1]*)[ |=|<|<=|>|>=|or|in]*.*$/', $input_lines, $output_array);
Some example SQL-queries:一些示例 SQL 查询:
SHOULD RETURN
WHERE var1=1 AND var2=1 => var1; var2
WHERE var2 IN (1, 2, 3) OR var3=2 => var2; var3
WHERE (var4 like '%test%' and var5 NOT LIKE '%test%') => var4; var5
WHERE VAR6 = 'test' AnD var7='test2' => VAR6; var7
Here we go (of course not a 100% perfect):这里我们 go(当然不是 100% 完美):
(?:(?i)WHERE|AND|OR) \(?([a-zA-Z0-9]+)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.