Sultan Sultan - 3 months ago 16
Java Question

regex to extract software version

I tried with this regex

(?!\\.)(\\d+(\\.\\d+)+)([-.][a-zA-Z]+)?(?![\\d.])
using Java code to extract software version number form short text as following:

... Dashbuilder before 0.6.0.Beta allows remote ... another version 0.6.1 which ...


However, I can extract
0.6.0
and
0.6.1
only without the letters Beta.

Update
Sometimes, the software version comes with digits and characters. How could I update my regex to be able to extract software versions such as digits with letters (
0.6.0.beta
), or digits only (
0.6.1
),
Also, what if I want to extract the term
before
if it is preceding the version number using regex?

rD. rD.
Answer

You can use

((?:\d.)+)

Test String

... Dashbuilder before 0.6.0.Beta allows remote  ...

Match Information

MATCH 1
1.  [23-29] `0.6.0.`

DEMO: https://regex101.com/r/pW8gO5/1


EDIT

To extract before 0.6.0.Beta use:

(\b\w+\s(?:\d.)+\w+\b)

Test String

... Dashbuilder before 0.6.0.Beta allows remote  ...

Match Information

MATCH 1
1.  [16-33] `before 0.6.0.Beta`

DEMO: https://regex101.com/r/pW8gO5/2


EDIT 2

You can use ? to match optional before word:

((?:before)?\s(?:\d.)+\w+\b)

Test String

... Dashbuilder before 0.6.0.Beta allows remote  ...

... Dashbuilder 0.6.0.Beta allows remote  ...

Match Information

MATCH 1
1.  [16-33] `before 0.6.0.Beta`
MATCH 2
1.  [69-80] ` 0.6.0.Beta`

DEMO: https://regex101.com/r/pW8gO5/3


EDIT 3

Updated to match the version too.

((?:before)?\s(?:[\d.])+[\w-]+)\b

Test String

... Dashbuilder before 0.6.0.Alpha allows remote  ...
... Dashbuilder before 0.6.0.Beta allows remote  ...
... Dashbuilder before 0.6.0.Beta allows remote  ...
... Dashbuilder before 0.6.0 allows remote  ...
... Dashbuilder before 0.6.0.SNAPSHOT allows remote  ...
... Dashbuilder before 0.6.0.RC allows remote  ...
... Dashbuilder before 0.6.0-RELEASE allows remote  ...

Match Information

MATCH 1
1.  [16-34] `before 0.6.0.Alpha`
MATCH 2
1.  [70-87] `before 0.6.0.Beta`
MATCH 3
1.  [123-140]   `before 0.6.0.Beta`
MATCH 4
1.  [176-188]   `before 0.6.0`
MATCH 5
1.  [224-245]   `before 0.6.0.SNAPSHOT`
MATCH 6
1.  [281-296]   `before 0.6.0.RC`
MATCH 7
1.  [332-352]   `before 0.6.0-RELEASE`

DEMO: https://regex101.com/r/pW8gO5/4

Comments