$re = '/^(\w+) =.*\n(?=((?!\b\1\b)[^!])*(^\1 =|\Z))/m';
$str = 'a = 10
a * 3
foo = 8
2 - 1
b = 18
c = 10
d = 8
d = 4
cd = 72
d / 6
d + 1
e = 1
e = 2
e + 1
FooBar1 = 0
Fuz__ = 8
Fuz__ / 1
f = 1
f + 1
f = 2
f + 1
g = 1
1 / 5 * 8 + 4';
$subst = "";
$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