$re = '~(?P<key>\b[A-Z]+\b) # key group
\h*=\h* # spaces, = spaces
(?P<value> # value group
(\[)? # if the first char is a [
(?(3).*?\] # ... look for ]
| # else
\w+ # [a-zA-Z0-9_]
)
)~x';
$str = 'M=NS MAX=[5,10,20,20] A=4 MIN=[1,1,1,20] R=1 S=1 T=3 D=2
M=NS MAX=[5,10,20,20] MIN=[1,1,1,20] R=1 S=1 T=3 A=4 D=2
M=NS MAX=[5,10,20,20] Z=123 MIN=[1,1,1,20] R=1 S=1 T=3 A=4 D=2 ';
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