如何使用Regex查找最新版本号? 例如
3.0.0.0& 3.1.0.0
4.0.1.0& 4.0.1.2
问候
庵埠
答案 0 :(得分:11)
为什么要使用正则表达式?他们没有进行比较。但是,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)));
}
答案 1 :(得分:0)
试试这个
static class Program
{
static void Main()
{
string v1 = "1.23.56.1487";
string v2 = "1.24.55.487";
var version1 = new Version(v1);
var version2 = new Version(v2);
var result = version1.CompareTo(version2);
if (result > 0)
Console.WriteLine("version1 is greater");
else if (result < 0)
Console.WriteLine("version2 is greater");
else
Console.WriteLine("versions are equal");
return;
}
}
答案 2 :(得分:-4)
好的版本号只是一个数字..比较3.0.0.0&amp; 3.1.0.0就像比较3000和3100最简单的那样:
int v1 = 3000;
int v2 = 3100;
if (v2 > v1) { my routine; }