$re = '/(?<=where)(.*?)((ORDER BY|GROUP BY|HAVING|LIMIT|OFFSET|$).*)/si';
$str = 'select "DIM_1"."col1",
"DIM_2"."col2" ,
"DIM_3"."col3" ,
"DIM_4"."col4" ,
"FAT_1"."col5"
from "FAT_1",
"DIM_1",
"DIM_2",
"DIM_3",
"DIM_4"
where "DIM_1"."col1" IS NOT NULL
AND "DIM_2"."col2" LIKE (\'SUCCESS\')
AND "DIM_3"."col3" BETWEEN 20161213 AND 20161222
AND "DIM_4"."col4" > 0
HAVING DIM_1.col1 > 2';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
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