$re = '/.*?(?<=(?:FROM|JOIN))\s*(.*?)\s*(?=(?:ON|JOIN|WHERE|LIMIT|GROUP|ORDER|\(|;))/';
$str = 'SELECT * FROM (SELECT * FROM table1, table2 WHERE bla,bla GROUP BY bla ORDER bla LIMIT bla) tableTmp UNION SELECT * FROM table3,table4 WHERE bla;';
$subst = "\1";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php