$str='SELECT "SELECT * FROM table1 WHERE ..." SQL FROM table2 WHERE'; print "$1\n" while $str =~ /(\bFROM\b.*?\bWHERE\b)/g; #### FROM table1 WHERE FROM table2 WHERE