MrBi
MrBi

Reputation: 328

How can extract string matches using regular expression from a List?

I have a List contain some strings inside like this and other data.

  HwndWrapper[App.exe;;cda6c3f4-8c87-4b12-8f3d-5322ca90eeex]
  HwndWrapper[App.exe;;cadac3f4-8c87-4b12-8q3d-1qwe2ca90eec]
  HwndWrapper[App.exe;;c1b6a3s4-8c87-4b12-8f3d-2qw2ca90eeev]

My list: // Returns a list of WindowInformation objects with Handle, Caption, Class, // Parent, Children, Siblings and process information

 List<WindowInformation> windowListExtended = WindowList.GetAllWindowsExtendedInfo();

The regular expresion to match is:

  HwndWrapper\[App.exe;;.*?\]

Now for every match on the list. I need extract the string matched and run a process with every string extracted, Foreach or something like that.

Some help please.

Update: Thanks Altaris for the help, just need convert List to string

        var message = string.Join(",", windowListExtended);
        string pattern = @"HwndWrapper\[LogiOverlay.exe;;.*?]";
        MatchCollection matches = Regex.Matches(message, pattern);

Upvotes: 1

Views: 455

Answers (2)

Valeriu Seremet
Valeriu Seremet

Reputation: 568

From what I understand you want to extract every match in a separate list to work with, there you go:

            var someList = new List<string>{"HwndWrapper[App.exe;;cda6c3f4-8c87-4b12-8f3d-5322ca90eeex]",
                                    "HwndWrapper[App.exe;;cadac3f4-8c87-4b12-8q3d-1qwe2ca90eec]",
                                    "HwndWrapper[App.exe;;c1b6a3s4-8c87-4b12-8f3d-2qw2ca90eeev]"};

            Regex FindHwndWrapper = new Regex(@"HwndWrapper\[App.exe;;(.*)\]");

            var matches = someList.Where(s => FindHwndWrapper.IsMatch(s)).ToList();

            foreach(var match in matches)
            {
                Console.WriteLine(match);// Use values
            }

I used System.Linq function Where() to iterate through list

Use this Linq line if you want just the id parts, like "cda6c3f4-8c87-4b12-8f3d-5322ca90eeex"

var matches = someList.Select(s => FindHwndWrapper.Match(s).Groups[1]).ToList();

Upvotes: 2

Furkan &#214;zt&#252;rk
Furkan &#214;zt&#252;rk

Reputation: 1416

I am unsure of what you want exactly, I think you want to extract these

        List<string> windowListExtended = new List<string>();
        windowListExtended.Add("HwndWrapper[App.exe;;cda6c3f4-8c87-4b12-8f3d-5322ca90eeex]");
        windowListExtended.Add("HwndWrapper[App.exe;;cadac3f4-8c87-4b12-8q3d-1qwe2ca90eec]");
        windowListExtended.Add("HwndWrapper[App.exe;;c1b6a3s4-8c87-4b12-8f3d-2qw2ca90eeev]");

        var myRegex = new Regex(@"HwndWrapper\[App.exe;;.*?]");
        var resultList = files.Where(x => myRegex.IsMatch(x)).Select(x => x.Split(new[] { ";;","]" }, StringSplitOptions.None)[1]).ToList();

        //Now resultList contains => cda6c3f4-8c87-4b12-8f3d-5322ca90eeex, cadac3f4-8c87-4b12-8q3d-1qwe2ca90eec, c1b6a3s4-8c87-4b12-8f3d-2qw2ca90eeev
        foreach (var item in resultList)
        {
            //Do whatever you want
        }

Upvotes: 1

Related Questions