use strict;
my $str = '#good
0.1.2
1.2.3
10.20.3
1.2.3-alpha.23-pre
12.12.3-123.hexagon+dontmakemecompileplea.se
1.2.3-alpha-dev.51-something+mybuild-1-4-1975-clang
4.3.22+mybuild
4.1.405+hexa.13331-objectfiles
# bad
01.2.3
1.02.3
2.3.04
a.1.1
1.a.1
1.1.a
1.2.3-rele..ase+build
1.2.3-release-something+..build
1.2.3-rele--ase+build
1.2.3-release-something+--build
1.2.3-release++build
1.2.3+-release-something-build';
my $regex = qr/^(?!.*\-{2}.*)(?!.*\+{2}.*)(?!.*\.{2}.*)(?P<major>(?!0)(\d*)|([0[^\d]))((?>\.)(?P<minor>\g<1>))\.(?P<patch>\g<minor>)(?![\+\-][^a-zA-Z0-9])(\g<2>(\-(?P<release>[a-zA-Z0-9\.-]+)))?(\+(?P<build>\g<9>))?$/mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
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 Perl, please visit: http://perldoc.perl.org/perlre.html