$re = '/\b(.*?])\s(__declspec.+?\)\))(.*?;)/m';
$str = '#if defined (_MSC_VER) && !defined(__clang__)
double pU0[3*4*K_MAX_STACK] __declspec(align(64));
#else
double pU0[3*4*K_MAX_STACK] __attribute__ ((aligned (64)));
#endif
#if defined (_MSC_VER) && !defined(__clang__)
double CC[16] __declspec(align(64)) = {0};
#else
double CC[16] __attribute__ ((aligned (64))) = {0};
#endif
#if defined (_MSC_VER) && !defined(__clang__)
double pU[3*4*K_MAX_STACK] __declspec(align(64));
#else
double pU[3*4*K_MAX_STACK] __attribute__ ((aligned (64)));
#endif';
$subst = "$2 $1$3";
$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