$re = '/^(ABC)_([0-9]{4})_([0-9]+\.[0-9]+\.[0-9]+)(_[a-zA-Z])?(_[a-zA-Z]+)?\.xyz$/m';
$str = 'ABC_0000_0.0.0.xyz
Group 1 : ABC
Group 2 : 0000
Group 3 : 0.0.0
Group 4 : <empty>
Group 5 : <empty>
ABC_0001_0.1.0_N.xyz
Group 1 : ABC
Group 2 : 0001
Group 3 : 0.1.0
Group 4 : _N
Group 5 : <empty>
ABC_0002_1.1.2_foo.xyz
Group 1 : ABC
Group 2 : 0002
Group 3 : 1.1.2
Group 4 : <empty>
Group 5 : _foo
ABC_0002_42.42.42_N_bar.xyz
Group 1 : ABC
Group 2 : 0002
Group 3 : 42.42.42
Group 4 : _N
Group 5 : _bar';
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