Reputation: 6308
Say a file opened in Eclipse has the following string
$stmt = "select addr from
student
where id=123";
$stmtA = "alter table tablename";
$stmtB = " delete from student
where school=ABC";
$var1 = "This is not a query.
Just a string";
I need to find all query statements that effect student table and school column. Searching with :
(?s)"(.*?)"
gives me all the strings that are quoted and spread over multiple lines. Now how do I enhance the above regex to filter the result so that it ensures that result has
1) select or alter or insert or delete keywords of MySQL, and 2) student and school keywords.
I think with above two conditions satisfied I will be able to extract strings that hit student table and school column. Any help?
Upvotes: 2
Views: 487
Reputation: 8293
(?s)".*?(?:select|alter|insert|delete).*?(?:student|school).*?"
Though using [^"]*?
instead of .*?
would maybe be better.
Edit:
Let's switch to lookaheads as they are quite the cool tool when ensuring some conditions (as string length, having a special char or smthg):
(?s)".*?(?:select|alter|insert|delete)(?=[^"]*?student)(?=[^"]*?school).*?"
Ok if you're not interested in regex, you can stop here, else, as an example of lookaheads (note: this is slower):
(?s)"(?=[^"]*?(?:select|alter|insert|delete)(?=[^"]*?student)(?=[^"]*?school).*?"
If you have access to atomic groups, it's always better to do this (atomic grouping):
(?>select|alter|insert|delete)
as if one word fails to match after the 1st letter, it skips the rest (they all have a different first letter).
Finally, I guess you could use if/then/else:
(?s)".*?(?:select|alter|insert|delete).*?(?:(student)|school)(?(1).*?school|.*?student).*?"
Or something similar.
Upvotes: 2