gpt4 book ai didi

c# - 使用正则表达式比较两个软件版本号

转载 作者:行者123 更新时间:2023-11-30 14:16:07 24 4
gpt4 key购买 nike

如何使用正则表达式查找最新版本号? 例如

3.0.0.0 & 3.1.0.0

4.0.1.0 & 4.0.1.2

问候

暗部

最佳答案

为什么要使用正则表达式?他们不进行比较。然而,System.Version不仅会解析字符串,而且还支持比较:

// Use your favorite comparison operator
var current = new Version("4.0.1.2");
var found = new Version("4.0.1.0");
if (found > current)
{
Console.WriteLine("Upgrade needed to {0} from {1}", found, current);
}
else
{
Console.WriteLine("No upgraded needed from {0}", current);
}

或者,如果您将它们放在枚举中,它可以很好地与 LINQ 配合使用:

var versions = new [] { "3.0.0.0", "3.1.0.0", "4.0.1.0", "4.0.1.2" };
foreach (var version in versions.Select(Version.Parse)
.OrderByDescending(v => v))
{
Console.WriteLine("{0}", version);
}

// Group them by Major Version first, then sort
foreach (var major in versions.Select(Version.Parse)
.GroupBy(v => v.Major)
.OrderByDescending(g => g.Key))
{
Console.WriteLine("{0}: {1}",
major.Key,
String.Join(", ", major.OrderByDescending(v => v)));
}

关于c# - 使用正则表达式比较两个软件版本号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8241141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com