$re = '/(?P<conditional>(?:(?:(?:"(?:(?:\\\\")|[^"\n])*")|(?:\'(?:(?:\\\\\')|[^\'\n])*\'))|(?:==|>=|<=)?[^=\?\n\(\)]|\((?1)\))*)\?(?P<true>[^{}\n:]*)\:(?P<else>[^{}\n:]*)(?:;|\))/mi';
$str = '#include <iostream>
int main() {
int a, b;
a = b = 0;
std::cout << "Enter a number: ";
std::cin >> a;
std::cout << "\\nEnter a number: ";
std::cin >> b;
std::cout << "Largest: " << ((a > b) ? a : b) << "\\nSmallest: " << ((a < b) ? a : b) << std::endl;
return 0;
}
void exec(std::string args, bool block) {
pid_t pid = fork(); // This process executes a given program
((pid == -1) ? ((pid == 0) ? child(args) : parent(pid, block)) : error("fatal fork error", true));
}
some_val = x >= 0 ? x : ( y >= 0 ) ? y : z;
(a>b) ? modifyB(b) : modifyA(a);';
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