开发者

Regular expression for version numbers

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.


What about this:

\d+(?:\.\d+)+


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


Try this:

[0-9]+\.[0-9]+(?:\.[0-9]+)?


I think this should make more sense:

^(?:0|[1-9][0-9]*)(?:\.(0|[1-9][0-9]*))*$

Every numeric component have to start with non-zero if it's more than 1 digits.


This might be an old topic, but I found the same problem and found somewhat simpler solution so this might help someone looking for an answer. This (\[0-9\]+\[\.\]?)+ will match these:

7.1.1
12.1.1.3.12.442.5463.343.44.5
9
11.1.1
11.1.1

additionally if someone would like to get only the numbers (alternatively to split on dot) this will do the trick:

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

(regexhero)


Didn't test it thoroughly but that's it folks :-)

(?<major>\d+)(?<minor>:\.\d+)?(?<build>:\.\d+)?(?<revision>:\.\d+)?


I recommend this one:

^\d+(?:\.\d+){2}$

{2} ensures that versions like 10.5 not match

^ and $ will "protect" the beginning and the end of the version, preventing some situations like :

A10.5.1
10.5.
10.5.1A


(\d+\.){1,3}\d+

Will match the following
Express Edition (64-bit) RTM 15.0.4223.1

15.0.4223.1
15.0.4
15.0

and Not match the following

15
15.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜