$re = '/(?:^.+\n)*abc (\d+)|^(x)(?==)/m';
$str = 'x=0
abc 20
def 76
ghi 11
x=1
def 45
ghi 32
x=2
jkl 56
mno 76
abc 134
.
.
.
x=393
xyz 21
abc 9
x=394
ghi
def
xyz';
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