In my java file there are many sql queries assigned to java strings like:
/* ... */
String str1 = "SELECT item1, item2 from table1 where this=that and MYWORD=that2 and this3=that3";
/* ... */
String str2 = "SELECT item1, item2 from table1 where this=that and" +
" MYWORD=that2 and this3=that3 and this4=that4";
/* ... */
/* ... */
String str3 = "SELECT item1, item2 from table2 where this=that and this2=that2 and" +
" this3=that3 and this4=that4";
/* ... */
String str4 = "SELECT item1, item2 from table3 where this=that and MYWORD=that2" +
" and this3=that3 and this4=that4";
/* ... */
String str5 = "SELECT item1, item2 from table4 where this=that and this2=that2 and this3=that3";
/* ... */
Now I want to find out the ‘SELECT…’ queries that doesn’t contain the word ‘MYWORD’ in it.
From one of my previous S/O question I got the answer how to find all the ‘SELECT...‘ queries, but I need to extend that solution to find the ones that doesn’t contain certain word.
I have tried the regex SELECT(?!.*MYWORD).*; that can’t find the multiline queries (like str3 above), finds only the single line ones.
I’ve also tried the regex SELECT[\s\S]*?(?!MYWORD).*(?<=;)$ that finds all the queries and is unable to determine whether the word ‘MYWORD’ is present in the query or not.
I know I’m very near to the solution, still can’t figure it out.
Can anyone help me, please?
(I am using notepad++ on windows)
The problem with the first regex is that
.doesn’t match a newline. In normal regexes, there is an option to change that, but I don’t know whether that feature exists in notepad++.The problem with the second regex is that is matches “select, then some stuff, then anything that doesn’t match MYWORD, then more stuff, then a semicolon” Even if MYWORD exists, the regex engine will happily match
(?!MYWORD)to some other part of the string that is not MYWORD.Something like this should work (caveat: not tested on Notepad++):
Instead of
., match anything that is not a semicolon. This should allow you to match a newline.Beyond that, it is also important that you don’t allow a semicolon to be part of the match. Otherwise, the pattern can expand to gobble up multiple
SELECTstatements as it tries to match.