【问题标题】:How can extract string matches using regular expression from a List?如何使用正则表达式从列表中提取字符串匹配?
【发布时间】:2019-11-11 06:30:24
【问题描述】:

我有一个列表,其中包含一些字符串和其他数据。

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

我的清单: // 返回 WindowInformation 对象的列表,其中包含 Handle、Caption、Class、 // 父、子、兄弟和进程信息

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

要匹配的正则表达式是:

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

现在对于列表中的每场比赛。我需要提取匹配的字符串并使用提取的每个字符串运行一个进程,Foreach 或类似的东西。

请帮忙。

更新: 感谢 Altaris 的帮助,只需要将 List 转换为字符串

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

【问题讨论】:

  • 你的正则表达式应该是这样的: HwndWrapper[App\.exe;;(.*?)] 然后提取的字符串可以被 Group 1 检索
  • 我找不到太多关于 WindowInformation 对象以及如何使用它的信息,也许这可以帮助你stackoverflow.com/questions/7268302/…
  • 窗口是非活动的,非标准窗口,没有名称,只有类以相对简单的方式可用。 ;(感谢您的帮助。
  • 我还是不明白你从哪里得到这个字符串 "HwndWrapper[App.exe;;cda6c3f4-8c87-4b12-8f3d-5322ca90eeex]"
  • 我从所有窗口列表中提取。无论如何,您的解决方案就像一个魅力,非常简单,只需转换为字符串 var message = string.Join(",", windowListExtended);字符串模式 = @"HwndWrapper[LogiOverlay.exe;;.*?]"; MatchCollection 匹配 = Regex.Matches(message, pattern);非常感谢您的帮助!

标签: c# regex list


【解决方案1】:

据我了解,您希望将每个匹配项提取到单独的列表中以供使用,您可以:

            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
            }

我使用System.Linq函数Where()遍历列表

如果您只需要 id 部分,请使用此 Linq 行,例如“cda6c3f4-8c87-4b12-8f3d-5322ca90eeex”

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

【讨论】:

  • 道歉我更新了信息。我的清单是 List
  • 更多信息在那里。
【解决方案2】:

我不确定你到底想要什么,我想你想提取这些

        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
        }

【讨论】:

  • 请原谅,现在再次使用工具,我会更新更多信息。 List no is is List
  • 我还是不明白你的代码逻辑是什么。我认为你应该更清楚。 @MrBi
猜你喜欢
  • 2022-10-01
  • 1970-01-01
  • 2020-12-15
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多