Just to toss out another approach, if you need to provide different behavior for different versions you could do this:
string pattern = @"Android (?<major>\d+)\.(?<minor>\d+)(\.(?<revision>\d+))?";
Match match = Regex.Match(text, pattern);
if (match.Success)
{
int major = int.Parse(match.Groups["major"].Value);
int minor = int.Parse(match.Groups["minor"].Value);
int revision;
if (!int.TryParse(match.Groups["revision"].Value, out revision))
revision = 0;
if (major == 2 && minor == 2)
{
if (revision == 0)
{
// Process version 2.2
}
else
{
// Process version 2.2.x, where x > 0
}
}
}
That will match a major version followed by a minor version optionally followed by a revision, and give you access to each of those three numbers. Note that if no revision is specified it's treated the same as if it were 0 (i.e. "Android 2.2" == "Android 2.2.0").
You can also use the Version class to do some of the work, like this:
string pattern = @"Android (?<version>\d+\.\d+(\.\d+)?)";
Match match = Regex.Match(text, pattern);
if (match.Success)
{
Version version = Version.Parse(match.Groups["version"].Value);
if (version.Major == 2 && version.Minor == 2)
{
if (version.Build < 1)
{
// Process version 2.2
}
else
{
// Process version 2.2.x, where x > 0
}
}
}
There the third number component is called "Build", not "Revision". Note that the Build property returns 0 if the value is 0 and -1 if the value is not specified.
\d{1}if you just need one digit,\dwill do.