Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#.NET regex not working as expected

Tags:

c#

.net

regex

Maybe It's because I'm totally fried right now, but this code:

static void Main(string[] args)
    {
        Regex regx = new Regex(@"^.*(vdi([0-9]+\.[0-9]+)\.exe).*$");
        MatchCollection results = regx.Matches("vdi1.0.exe");
        Console.WriteLine(results.Count);

        if (results.Count > 0)
        {
            foreach (Match r in results)
            {
                Console.WriteLine(r.ToString());
            }
        }
    }

ought to produce the output:

2
vdi1.0.exe
1.0

if I'm not crazy. Instead, it's just producing:

1
vdi1.0.exe

What am I missing?

like image 209
Bodacious Avatar asked Feb 16 '23 21:02

Bodacious


1 Answers

Your Regular Expression will only return one Match object with 2 subgroups. You can access those groups using the Groups collection of the Match object.

Try something like:

foreach (Match r in results) // In your case, there will only be 1 match here
{
   foreach(Group group in r.Groups) // Loop through the groups within your match
   {
      Console.WriteLine(group.Value);
   }
}

This allows you to match multiple filenames in a single string, then loop through those matches, and grab each individual group from within the parent match. This makes a bit more sense than returning a single, flattened out array like some languages. Also, I'd consider giving your groups names:

Regex regx = new Regex(@"^.*(?<filename>vdi(?<version>[0-9]+\.[0-9]+)\.exe).*$");

Then, you can refer to groups by name:

string file = r.Groups["filename"].Value;
string ver = r.Groups["version"].Value;

This makes the code a bit more readable, and allows group offsets to change without breaking things.

Also, if you're always parsing only a single filename, there's no reason to loop through a MatchCollection at all. You can change:

MatchCollection results = regx.Matches("vdi1.0.exe");

To:

Match result = regx.Match("vdi1.0.exe");

To obtain a single Match object, and access each Group by name or index.

like image 144
Mike Christensen Avatar answered Feb 23 '23 17:02

Mike Christensen