$re = '/\x1B\[(?:;?[0-9]{1,3})+[mGK]/m';
$str = 'Summary [0m
Accounts:
Name Account ID Title
----------------------------------------------- ------------ -----------------------------------------------
[32maccount-accountType-gamma[0m [38;2;128;128;128m123456789012[0m account-accountType-gamma
[32maccount-accountType-e2e-gamma[0m [38;2;128;128;128m123456789012[0m account-accountType-e2e-gamma
Summary [0m
Accounts:
Name Account ID Title
---------------------------------------------- ------------ ----------------------------------------------
[32maccount-accountType-e2e-prod[0m [38;2;128;128;128m123456789012[0m account-accountType-e2e-prod
[32maccount-accountType-prod[0m [38;2;128;128;128m123456789012[0m account-accountType-prod';
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