Regex pattern to extract version number from string

Try:

Regex pattern = new Regex("\d+(\.\d+)+");
Match m = pattern.Match(a);
string version = m.Value;

\d+(\.\d+)+

\d+         : one or more digits
\.           : one point
(\.\d+)+ : one or more occurences of point-digits

Will find

2.5
3.4.567
3.4.567.001

But will not find

12
3.
.23

If you want to exclude decimal numbers like 2.5 and expect a version number to have at least 3 parts, you can use a quantifier like this

\d+(\.\d+){2,}

After the comma, you can specify a maximum number of ocurrences.

Tags:

C#

Regex