简体   繁体   English

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

[英]Regex pattern to match and replace a version number in a string c#

I want to find replace Version number from a string by using Regex in c#. 我想通过在c#中使用Regex从字符串中找到替换版本号。

string is like this: 字符串是这样的:

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

Is any one can help me to solve this problem. 有谁能帮助我解决这个问题。

It seems to me you could easily accomplish this without a regex and have your code be easier to read: 在我看来,您无需使用正则表达式就可以轻松完成此操作,并使代码更易于阅读:

 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):

The following Regex will do the replace you are looking for. 以下正则表达式将执行您要查找的替换。

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");

EDIT: You can also use look-behind Regex functionality if you don't want to have to put the "Version=" in the replacement string. 编辑:如果您不想将“ Version =”放在替换字符串中,则还可以使用后向Regex功能。 If you are looking for that, add a comment and I'll draw it up. 如果您正在寻找,请添加评论,然后我将其草拟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM