Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for version numbers

Tags:

c#

regex

I want to get such matches:

3.1.0
10.5.1
0.5

which may represent builds version numbering system.

Non-matches:

3.1.

I tried this regex:

[0-9]+\.[0-9]+

This gets only 0.5 but not 10.5.1.

like image 377
Ken D Avatar asked Jul 08 '11 01:07

Ken D


2 Answers

What about this:

\d+(?:\.\d+)+
like image 151
zerkms Avatar answered Sep 28 '22 18:09

zerkms


How about this?

^\d{1,3}\.\d{1,3}(?:\.\d{1,6})?$

This will match Major.Minor and optional revision. Major and Minor can be 1-3 digits (0-999) and Revision can be 6 digits.

Valid: 1.1 1.2.3 1.2.123456

Not valid: 1 1.2. 1.2.1234567 1.2.* Anything with a alpha character

like image 28
FlexibleCoder Avatar answered Sep 28 '22 18:09

FlexibleCoder