$re = '/(?(DEFINE)
(?<add> \s*\+\s* )
(?<eq> \s*=\s* )
(?<carry> (?(cl)(?(cr)|\d0)|(?(cr)\d0|(*F))) )
(?<b>
(?&add) (?:\d*(?:0|1(?<r1>)))? (?(ro)|(?=(?<cr>1)?)) (?=\k<e> (?&eq) \d*(?(?=(?(l1)(?(r1)|(*F))|(?(r1)(*F))))(?(?=(?&carry))1|0)|(?(?=(?&carry))0|1))\k<f>\b)
| (?=\d* (?&add) (?:\k<e>(?<ro>)|\d*(?<e>\d\k<e>)) (?&eq) \d*(?<f>\d\k<f>)\b) \d(?&b)
)
(?<s> (?:0|1(?<l1>))(?=(?<cl>1)?\d*(?<e>) (?&add) \d* (?&eq) \d*(?<f>)\b)(?&b) )
)
\b(?=(?:(?=(?&s))\d)+)[01]+(?&add)[01]+(?&eq)[01]+\b/xJ';
$str = '1 + 1 = 1
0 + 1 = 1
01 + 10 = 11
010 + 010 = 100
101 + 10 = 111
101 + 10 = 101
11 + 101 = 1000
010 + 010 = 000
';
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