Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何匹配一个模式,该模式可以是字符串中的任意位置,但不能在两个单词之间?_Regex_Perl - Fatal编程技术网

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)您尝试过答案吗,这对您有效吗?