$a = qq[SELECT "SELECT * FROM table1 WHERE ..." SQL FROM table2 WHERE ...];; $a =~ /(?:"[^"]+")?.*(\bFROM\b.*?\bWHERE\b)/g and print $1;; FROM table2 WHERE