vdenotaris
vdenotaris

Reputation: 13637

Regex for Java version number

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?

An alphanumeric string could follows the version number, but only if there is a . or - (just one).

Upvotes: 2

Views: 9239

Answers (4)

Cristiano
Cristiano

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

tsogtgerel.ts
tsogtgerel.ts

Reputation: 975

^\d+\.\d+\.\d+[\.\-]?[A-Z]*$

for java replace "\" to "\\"

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174706

Add a non-capturing group for .[A-Z]+ or -[A-Z]+ part and make it as optional.

(?!\.)(\d+(\.\d+)+)(?:[-.][A-Z]+)?(?![\d.])$

DEMO

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.])$

DEMO

To capture and strore only the letters into separate group.

DEMO

(?!\.)(\d+(\.\d+)+)(?:[-.]([A-Z]+))?(?![\d.])$

Upvotes: 6

Amit Joki
Amit Joki

Reputation: 59252

Just use this less complicated regex

/(\d.){2}\d(?=((-|\.)[A-Z]+)|$)/g

DEMO

Upvotes: 2

Related Questions