用于匹配和替换字符串c#中的版本号的正则表达式模式

时间:2011-08-24 14:45:38

标签: c# regex

我想在c#中使用Regex从字符串中找到替换版本号。

字符串是这样的:

string val =“AA.EMEA.BizTalk.GroupNettingIntegrations,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = a86ac114137740ef”;

是否有人可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:3)

在我看来,如果没有正则表达式,您可以轻松完成此操作,并且您的代码更易于阅读:

 string components[] = someAssemblyFullyQualifiedName.Split(new char[] { ',' }, StringSplitOptions.IgnoreEmptyEntires);

 if(components.Length > 1)
    components[1] = "Version=2.0.0.0"; // whatever you want to replace with

 string newFullyQualifiedName = string.Join(",", components):

答案 1 :(得分:1)

以下Regex将执行您正在寻找的替换。

string val = "AA.EMEA.BizTalk.GroupNettingIntegrations, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a86ac114137740ef";
val = Regex.Replace(val, @"Version=[\d\.]+", "Version=2.0.0.0");

编辑: 如果您不想将“Version =”放在替换字符串中,也可以使用look-behind Regex功能。如果您正在寻找,请添加评论,我会画出来。