2017-06-13 53 views
2

說我兩個字符串,如何匹配可以在字符串中的任何位置但不在兩個字之間的模式?

$string1 = "select * from auditor where name in (select 'Jack' name from employee where id = 1)"; 

$string2 = "select * from employee where name = 'Jack'"; 

現在,我需要一個正則表達式只有在找到任何封閉的單引號內的WHERE子句。即在$ string1中它不應該匹配,因爲在select子句中使用單引號並且$ string2應該與在where子句中使用的匹配。

我試圖

(?!select .*\'(.*)\' where)where (.*\'(.*)\') 
+0

如果您接受重新構造這個問題,它可以「在anystring ='pattern」之後匹配單引號內的任何字符串。這解決了你的具體問題,但並沒有真正回答這個問題:'(?<= where)[\ w \ s] * = \ s *'(\ w *)'' – mquantin

回答

0

你可以試試這個方法:

​​

並得到結果在第1組

Regex Demo

對於以下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 

PS:PLZ注意,你不需要插入/更新/刪除這些被放置在正則表達式只是爲了讓更多的普通

+0

@Aiswarya你有沒有試過答案,爲你工作? –

相關問題