1

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);
MrBi
  • 308
  • 1
  • 14
  • Your regex should be like : HwndWrapper\[App\.exe;;(.*?)\] Then the extracted string can be retrieved by Group 1 – thang Nov 11 '19 at 06:44
  • I could not find much information about WindowInformation object and how to work with it, may be this can help you https://stackoverflow.com/questions/7268302/get-the-titles-of-all-open-windows – Valeriu Seremet Nov 11 '19 at 07:35
  • The window is non active, non standard window, with no name, only the class is available in relative easy way. ;( Thanks for your help. – MrBi Nov 11 '19 at 07:41
  • I still don't understand from where did you get this strings "HwndWrapper[App.exe;;cda6c3f4-8c87-4b12-8f3d-5322ca90eeex]" – Valeriu Seremet Nov 11 '19 at 07:51
  • I pickup form all window list. Anyway, your solution work like a charm, is very simple, just convert to string var message = string.Join(",", windowListExtended); string pattern = @"HwndWrapper\[LogiOverlay.exe;;.*?]"; MatchCollection matches = Regex.Matches(message, pattern); Thanks a lot for your help! – MrBi Nov 11 '19 at 07:57
  • Final comment, check the update :) Thanks a lot again!! work like charm now! – MrBi Nov 11 '19 at 08:00

2 Answers2

2

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();
Valeriu Seremet
  • 558
  • 1
  • 7
  • 17
1

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
        }
Furkan Öztürk
  • 1,178
  • 11
  • 24