$re = '/^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<preRelease>(?:[a-zA-Z1-9][a-zA-Z\d]*|0\d*[a-zA-Z][a-zA-Z\d]*|0)(?:\.(?:[a-zA-Z1-9][a-zA-Z\d]*|0\d*[a-zA-Z][a-zA-Z\d]*|0))*))?(?:\+(?<metadata>(?:[a-zA-Z\d-]*)(?:\.(?:[a-zA-Z\d-]*))*))?$/m';
$str = '0.1.0
01.2.1
1.2.0
1.0.0
1.01.0
1.0.01
1.0.0-0df34.ahgdfhsdf.0005a56tg
1.0.0-0054.df4gf
1.0.0-005a4.df4gf
1.0.0-005a4.df4gf+adffbff.sdfg.sdfg.adf78df7d8df7
1.0.0-005a4.df4gf+adffbff.sdfg.sdfg.00000000.dfsg.sdf-sfg.sdfg-sdfg.sgf
900.10.85494
0900.10.54';
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