【发布时间】:2011-02-25 23:31:49
【问题描述】:
在 AssemblyInfo.cs 文件中有这个字符串:[assembly: AssemblyVersion("1.0.0.1")],我正在尝试一一检索其中的数字,每个数字都是以下结构中的一个变量。
static struct Version
{
public static int Major, Minor, Build, Revision;
}
我正在使用这种模式来尝试检索数字:
string VersionPattern = @"\[assembly\: AssemblyVersion\(""(\d{1,})\.(\d{1,})\.(\d{1,})\.(\d{1,})""\)\]";
但是,当我使用此代码时,结果不是预期的那样,而是显示完整的字符串,就好像它是真正的匹配项,而不是组中的每个数字。
Match match = new Regex(VersionPattern).Match(this.mContents);
if (match.Success)
{
bool success = int.TryParse(match.Groups[0].Value,Version.Major);
...
}
在这种情况下,this.mContents 是从文件中读取的整个文本,match.Groups[0].Value 应该是 AssemblyVersion 中的“1”
我的问题是用正则表达式一一检索这些数字。
这个小工具可以在每次 Visual Studio 构建时增加应用程序版本,我知道有很多工具可以做到这一点。
【问题讨论】: