说我有两个字符串,
$string1 = "select * from auditor where name in (select 'Jack' name from employee where id = 1)";
$string2 = "select * from employee where name = 'Jack'";
现在,我需要一个RegEx来查找仅在where子句中包含在单引号内的任何内容。即,在$ string1中它不应该匹配,因为在select子句中使用单引号,并且$ string2应该匹配,因为它在where子句中使用。
我试过
(?!select .*\'(.*)\' where)where (.*\'(.*)\')
答案 0 :(得分:1)
如果查询可以任意复杂,正确的建议是使用解析器,因为SQL是上下文相关的,因此超出了正则正则表达式的能力。
如果where
子句总是很简单,则可以使用以下方式约束模式
if (/\bwhere\s+\w+\s+=\s+'([^']*)'/i) {
print " MATCH [$1]\n";
}
在where column = 'foo'
形式的子句中查找带引号的字符串。
答案 1 :(得分:0)
您可以尝试这种方法:
where(?!.*select|insert|update|delete).*?'([^']+)'
并在第1组中获得结果
对于以下3个输入:
$string1 = "select * from auditor where name in (select 'Jack0' name from employee where id = 1)";
$string2 = "select * from auditor where name in (blablabla 'Jack1' name from employee where id = 'jack2')";
$string3 = "select * from employee where name = 'Jack3'";
输出:
You will get jack1,jack2,jack3 in the first capture group but not won't get jack0
p.s:请注意,您不需要插入/更新/删除这些被放入正则表达式只是为了使它更通用