$re = '/(__|_e)\(\s?((\'|")(((?!(?<!\\\\)\3).)*)\3)(\s?|\s?(,\s?(\'\'|""))?)\)/m';
$str = '// i want to match these
__(\'Translate this\', \'\')
__( \'Translate this\' )
__( \'There\\\'s a translation\' )
__(\'Translate this\' )
_e(\'Translate this\')
esc_html_e(\'Translate this\')
esc_html__(\'Translate this\')
// i don\'t want to match these
__(\'Translate this\', \'my-text-domain\')
_e(\'Translate this\', \'my-text-domain\')
// not managing
_n( \'We deleted %d spam message.\', \'We deleted %d spam messages.\', $count, \'my-text-domain\' )
_x( \'Comment\', \'column name\', \'my-text-domain\' )
_ex( \'Comment\', \'column name\', \'my-text-domain\' );';
$subst = "$1( $2, 'YOUR-TEXT-DOMAIN' )";
$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