用于版本号解析的正则表达式 [英] A regex for version number parsing

查看:72
本文介绍了用于版本号解析的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下形式的版本号:

version.release.modification

其中版本、发布和修改是一组数字或*"通配符.此外,这些数字中的任何一个(以及任何前面的 .)可能会丢失.

因此以下内容有效并解析为:

1.23.456 = 版本 1,发布 23,修改 4561.23 = 版本 1,版本 23,任何修改1.23.* = 版本 1,版本 23,任何修改1.* = 版本 1,任何版本,任何修改1 = 版本 1,任何版本,任何修改* = 任何版本、任何版本、任何修改

但这些都是无效的:

*.12*123.112*12.*.34

谁能给我提供一个不太复杂的正则表达式来验证和检索版本号、版本号和修改号?

解决方案

我将格式表示为:

<块引用>

"1-3个点分隔的组件,每个数字除了最后一个可能是*"

作为正则表达式,那就是:

^(\d+\.)?(\d+\.)?(\*|\d+)$

I have a version number of the following form:

version.release.modification

where version, release and modification are either a set of digits or the '*' wildcard character. Additionally, any of these numbers (and any preceding .) may be missing.

So the following are valid and parse as:

1.23.456 = version 1, release 23, modification 456
1.23     = version 1, release 23, any modification
1.23.*   = version 1, release 23, any modification
1.*      = version 1, any release, any modification
1        = version 1, any release, any modification
*        = any version, any release, any modification

But these are not valid:

*.12
*123.1
12*
12.*.34

Can anyone provide me a not-too-complex regex to validate and retrieve the release, version and modification numbers?

解决方案

I'd express the format as:

"1-3 dot-separated components, each numeric except that the last one may be *"

As a regexp, that's:

^(\d+\.)?(\d+\.)?(\*|\d+)$

[Edit to add: this solution is a concise way to validate, but it has been pointed out that extracting the values requires extra work. It's a matter of taste whether to deal with this by complicating the regexp, or by processing the matched groups.

In my solution, the groups capture the "." characters. This can be dealt with using non-capturing groups as in ajborley's answer.

Also, the rightmost group will capture the last component, even if there are fewer than three components, and so for example a two-component input results in the first and last groups capturing and the middle one undefined. I think this can be dealt with by non-greedy groups where supported.

Perl code to deal with both issues after the regexp could be something like this:

@version = ();
@groups = ($1, $2, $3);
foreach (@groups) {
    next if !defined;
    s/\.//;
    push @version, $_;
}
($major, $minor, $mod) = (@version, "*", "*");

Which isn't really any shorter than splitting on "." ]

这篇关于用于版本号解析的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆