Reputation: 13637
I've a regex as follows:
(?!\.)(\d+(\.\d+)+)(?![\d\.])$
It valids strings as: 1.0.0
(software version).
How could I edit it in order to valid as well the following string?
1.0.0-SNAPSHOT
1.0.0.RC
1.0.0-RELEASE
An alphanumeric string could follows the version number, but only if there is a .
or -
(just one).
Upvotes: 2
Views: 9239
Reputation: 1813
Just in case someone need to capture each component of a semantic version this regex will help do this:
^(\d+)(?:\.(\d+)(?:\.(\d+))?)?(?:[-.]([A-Z]+))?(?![\d.])$
The major will be captured in the group 1, the minor in group 2, the incremental in group 3 and the qualifier in the group 4.
Upvotes: 0
Reputation: 174706
Add a non-capturing group for .[A-Z]+
or -[A-Z]+
part and make it as optional.
(?!\.)(\d+(\.\d+)+)(?:[-.][A-Z]+)?(?![\d.])$
If you want to capture the string part(Uppercase letters preceded by a dot or hyphen) then make the non-capturing group to capturing group.
(?!\.)(\d+(\.\d+)+)([-.][A-Z]+)?(?![\d.])$
To capture and strore only the letters into separate group.
(?!\.)(\d+(\.\d+)+)(?:[-.]([A-Z]+))?(?![\d.])$
Upvotes: 6