$re = '/(?<=SL{1}\d_)[0-9_]*|(?<=DM{1}\d\_)[0-9_]*/m';
$str = 'DM1_65_78109
SL1_78_72947
Input is DM1_813_1881
Input is DM2_793_1808
Input is DM1_SL1_609_604980
output should be 813_1881
output 609_604980
output 793_1808';
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