Your regex will only return one Match object with two subgroups. You can access these groups using the Groups collection of the Match object.
Try something like:
foreach (Match r in results)
This allows you to match multiple file names on a single line, then iterate over these matches and grab each individual group from the parent match. This is slightly better than returning a single, flattened array, like some languages do. In addition, I would name the group names:
Regex regx = new Regex(@"^.*(?<filename>vdi(?<version>[0-9]+\.[0-9]+)\.exe).*$");
Then you can access the groups by name:
string file = r.Groups["filename"].Value; string ver = r.Groups["version"].Value;
This makes the code more readable and allows you to change group offsets without breaking.
In addition, if you always parse only one file name, there is no reason to scroll MatchCollection at all. You can change:
MatchCollection results = regx.Matches("vdi1.0.exe");
To:
Match result = regx.Match("vdi1.0.exe");
To get a single Match object and access each Group by name or index.
source share