Regex 如何匹配一个模式,该模式可以是字符串中的任意位置,但不能在两个单词之间?
假设我有两条线Regex 如何匹配一个模式,该模式可以是字符串中的任意位置,但不能在两个单词之间?,regex,perl,Regex,Perl,假设我有两条线 $string1 = "select * from auditor where name in (select 'Jack' name from employee where id = 1)"; $string2 = "select * from employee where name = 'Jack'"; 现在,我需要一个正则表达式来查找仅包含在where子句中的单个引号中的任何内容。i、 例如,在$string1中,它不应该匹配,因为select子句中使用单引号,而$st
$string1 = "select * from auditor where name in (select 'Jack' name from employee where id = 1)";
$string2 = "select * from employee where name = 'Jack'";
现在,我需要一个正则表达式来查找仅包含在where子句中的单个引号中的任何内容。i、 例如,在$string1中,它不应该匹配,因为select子句中使用单引号,而$string2应该匹配,因为它在where子句中使用
我试过了
(?!select .*\'(.*)\' where)where (.*\'(.*)\')
您可以尝试以下方法:
where(?!.*select|insert|update|delete).*?'([^']+)'
并在第一组得到结果
对于以下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:plz请注意,您不需要插入/更新/删除这些被放在正则表达式中,只是为了使其更通用一点如果查询可以任意复杂,正确的建议是使用解析器,因为SQL是上下文敏感的,因此超出了sane正则表达式的能力 如果
where
子句总是简单的,那么您可以使用约束模式,例如
if (/\bwhere\s+\w+\s+=\s+'([^']*)'/i) {
print " MATCH [$1]\n";
}
要在
where column='foo'
格式的子句中查找带引号的字符串,如果您接受重新格式化问题,则可以是“在where anystring=
模式后匹配单引号内的任何字符串”。这解决了您的具体问题,但并没有真正回答问题:(?@Aiswarya)您尝试过答案吗,这对您有效吗?