I\'m trying to match the parts of a version number (Major.Minor.Build.Revision) with C# regular expressions. However, I\'m pretty new to writing Regex and even using Expresso is
Above answer not working properly
(?<Major>\d*)\.(?<Minor>\d*)(\.(?<Build>\d*)(\.(?<Revision>\d*))?)?
try this one,
var regularExpression = @"^(\d+\.)?(\d+\.)?(\d+\.)?(\*|\d+)$";
var regex = Regex.IsMatch("1.0.0.0", regularExpression, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
Console.WriteLine(regex);
(?<Major>\d*)\.(?<Minor>\d*)(\.(?<Build>\d*)(\.(?<Revision>\d*))?)?
Makes the third and fourth parts optional.
Try something like this:
(?<Major>\d*)\.?(?<Minor>\d*)?\.?(?<Build>\d*)?\.?(?<Revision>\d*)?
I simply added some "zero or one" quantifiers to the capture groups and also to the dots just in case they are not there.
I know this isn't a regex, but System.Version does all the work for you.
Tested in VBScript, this pattern:
^(\d+)(\.\d+)?(\.\d+)?(\.\d+)?$
Will validate all the following True:
And the following all False:
If you don't want to use Regex you could try:
System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(<string filePath>);
int major = fvi.FileMajorPart;
int minor = fvi.FileMinorPart;
int build = fvi.FileBuildPart;