$re = '/^(\w+)-(\w+)-(\w+)-(\d)$/ium';
$str = 'aaa-bbb-bbb-ccc-1
aaa-bbbbbb-cccc-1
a-bbbbbb-cccc-1
a-b-cccc-1
a-b-c-1
a-b-c
aaa_1-bbb_2-bbb_3-ccc_4-1
aaa_1-bbbbbb_2-cccc_3-1
a_1-bbbbbb_2-cccc_3-1
a_1-b_2-cccc_3-1
a_1-b_2-c_3-1
a_1-b_2-c_3';
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