简体   繁体   English

在visual studio中使用正则表达式查找和替换程序集

[英]Find and replace assembly with regex in visual studio

We have a solution with over 25 projects, each one with it's own AssemblyInfo I am trying to increment all of the assembly numbers to increase one major version for our next release. 我们有一个超过25个项目的解决方案,每个项目都有自己的AssemblyInfo我试图增加所有的程序集号,以增加我们下一个版本的一个主要版本。

My regex skills are minimal but essentially I want to find: 我的正则表达式技能很小,但基本上我想找到:

<Assembly: AssemblyVersion("x.x.x.x")>

Where x in regex terms will be \\d /a number. 正则表达式中的x将是\\d /数字。 Looking here I thought I could use: 这里我以为我可以使用:

{AssemblyVersion("[\d].[\d].[\d].[\d]")}

Which didn't work, it doesn't like the open brackets. 哪个没用,它不喜欢开括号。 My question is essentially broken down into two stages: 我的问题基本上分为两个阶段:

  1. What regex string will I use to find the example? 我将使用什么正则表达式字符串来查找示例?
  2. Is it possible to replace the first digit with the digit + 1 using regex? 是否可以使用正则表达式用数字+ 1替换第一个数字? (I suspect I am being a bit optimisitic) if so what will the regex string be in this situation? (我怀疑我有点乐观)如果是这样,那么正则表达式字符串在这种情况下是什么?

This is in source control, so I wouldn't like to modify the files directly. 这是源代码控制,所以我不想直接修改文件。

Thanks in advance. 提前致谢。

Note: this is using the Visual Studio regex find and replace. 注意:这是使用Visual Studio正则表达式查找和替换。

I don't think you can add one while doing a regex replace. 我不认为你可以在进行正则表达式替换时添加一个。 However, you could substitute a literal number. 但是,您可以替换文字编号。 For example, you could use a regex that replaces the first digit 1 with a 99 : 例如,您可以使用正则表达式将第一个数字1替换为99

AssemblyVersion("1.0.2121.0") --> AssemblyVersion("99.0.2121.0") AssemblyVersion("1.0.2121.0") - > AssemblyVersion("99.0.2121.0")

Here is the regex I used for the Visual Studio Search and replace: 这是我用于Visual Studio搜索和替换的正则表达式:

Search Regex:  AssemblyVersion[(]["]([0-9]+)([.][0-9]+[.][0-9]+[.][0-9]+)["][)]
Replace Regex: AssemblyVersion("99${2}")

The search regex escapes all special characters (.") by wrapping them in square brackets. Then it uses parenthesis to split the version number into two groups. The first group is the first set of digits. The second group is the last three sets of digits. 搜索正则表达式通过将它们包装在方括号中来转义所有特殊字符(.") 。然后使用括号将版本号拆分为两组。第一组是第一组数字。第二组是最后三组数字。

For example, AssemblyVersion("1.0.2121.0") is grouped like this (using braces for groupings): AssemblyVersion("{1}{.0.2121.0}") . 例如, AssemblyVersion("1.0.2121.0")分组(使用大括号进行分组): AssemblyVersion("{1}{.0.2121.0}") So group 1 is 1 , and group 2 is .0.2121.0 . 因此组1为1 ,组2为.0.2121.0

The replace regex AssemblyVersion("99${2}") uses a literal and group 2 from the search regex to build the replacement string. 替换正则表达式AssemblyVersion("99${2}")使用搜索正则表达式中的文字和组2来构建替换字符串。 The ${2} means use the text from group 2. The rest of the string is the literal. ${2}表示使用组2中的文本。字符串的其余部分是文字。

If you are going to use C# to find your text then I think this is the regex you will need - I have wrapped it up in a verbatim string and escaped the double quotes. 如果您打算使用C#查找文本,那么我认为这是您需要的正则表达式 - 我已将其包含在逐字字符串中并转义双引号。

Regex regex = new Regex(@"<Assembly: AssemblyVersion\(""\d+\.\d+\.\d+\.\d+""\)>");

If you want to pick elements to manipulate the string into a new string then add some brackets like so: 如果要选择元素来将字符串操作为新字符串,则添加一些括号,如下所示:

Regex regex = new Regex(@"<Assembly: AssemblyVersion\(""(\d+)\.(\d+)\.(\d+)\.(\d+)""\)>");

You can then access the numbers individually to create a new string using the regex groups: 然后,您可以单独访问这些数字以使用正则表达式组创建新字符串:

Match match = regex.Match(your_search_text);
if (match.Success)
{
    Console.WriteLine(match.Groups[1].Value);
    Console.WriteLine(match.Groups[2].Value);
    Console.WriteLine(match.Groups[3].Value);
    Console.WriteLine(match.Groups[4].Value);
}

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

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