使用Regex从AssemblyInfo.cs文件中检索程序集版本

6 .net c# regex match

在AssemblyInfo.cs文件中有这个字符串:[assembly: AssemblyVersion("1.0.0.1")]我试图逐个检索其中的数字,每个都是以下结构中的变量.

static struct Version
{
  public static int Major, Minor, Build, Revision;
}
Run Code Online (Sandbox Code Playgroud)

我正在使用此模式尝试检索数字:

string VersionPattern = @"\[assembly\: AssemblyVersion\(""(\d{1,})\.(\d{1,})\.(\d{1,})\.(\d{1,})""\)\]";
Run Code Online (Sandbox Code Playgroud)

但是,当我使用此代码时,结果不是预期的,而是显示完整的字符串,就好像它是真正的匹配而不是组中的每个数字.

Match match = new Regex(VersionPattern).Match(this.mContents);
if (match.Success)
{
  bool success = int.TryParse(match.Groups[0].Value,Version.Major);
  ...
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下this.mContents,从文件读取的整个文本match.Groups[0].Value应该是AssemblyVersion中的"1"

我的问题是用Regex逐个检索这些数字.

这个小工具是每次Visual Studio构建它时增加应用程序版本,我知道有很多工具可以做到这一点.

Joh*_*don 3

第一组正在播放完整比赛。您的版本号位于 1-4 组中:

int.TryParse(match.Groups[1].Value, ...)
int.TryParse(match.Groups[2].Value, ...)
int.TryParse(match.Groups[3].Value, ...)
int.TryParse(match.Groups[4].Value, ...)
Run Code Online (Sandbox Code Playgroud)