$re = '/(?P<release>(?:0|[1-9])\d*(?:\.(?:0|[1-9]\d*)){2})(?:-(?P<pre>(?P<prel>a|b|rc)(?P<pren>(?:0|[1-9])\d*)))?(?:\+(?P<local>[a-z\d]+(?:[-_\.][a-z\d]+)*))?/m';
$str = 'Valid PEP440 Verions
Final versions
0.9
0.9.1
0.9.2
0.9.10
0.9.11
1.0.1
2.0.1
Version epochs
1.0
1.1
2.0
2013.10
2014.04
2013.10
2014.04
1!1.0
1!1.1
1!2.0
Examples of compliant version schemes
Simple "major.minor" versioning:
0.1
0.2
0.3
1.0
1.1
Simple "major.minor.micro" versioning:
1.1.0
1.1.1
1.1.2
1.2.0
"major.minor" versioning with alpha, beta and candidate pre-releases:
0.9
1.0-a1
1.0-a2
1.0.7-b1
1.0.34-rc1
1.0
1.1.0-a1
"major.minor" versioning with developmental releases, release candidates and post-releases for minor corrections:
0.9
1.0.dev1
1.0.dev2
1.0.dev3
1.0.dev4
1.0c1
1.0c2
1.0
1.0.post1
1.1.dev1
Date based releases, using an incrementing serial within each year, skipping zero:
2012.1
2012.2
2012.3
2012.15
2013.1
2013.2
The following example covers many of the possible combinations:
1.0.dev456
1.0a1
1.0a2.dev456
1.0a12.dev456
1.0a12
1.0b1.dev456
1.0b2
1.0b2.post345.dev456
1.0b2.post345
1.0rc1.dev456
1.0rc1
1.0
1.0+abc.5
1.0+abc.7
1.0+5
1.0.post456.dev34
1.0.post456
1.1.dev1';
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