re = /^(?!.*\-{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>))?$/m
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'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
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 Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html