简体   繁体   English

如何使用正则表达式从列表中提取字符串匹配?

[英]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我的列表: // 返回 WindowInformation 对象的列表,其中包含 Handle、Caption、Class,//Parent、Children、Siblings 和进程信息

 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.我需要提取匹配的字符串并使用提取的每个字符串、Foreach 或类似的东西运行一个进程。

Some help please.请帮忙。

Update: Thanks Altaris for the help, just need convert List to string更新:感谢 Altaris 的帮助,只需要将 List 转换为字符串

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

From what I understand you want to extract every match in a separate list to work with, there you go:据我了解,您想在单独的列表中提取每个匹配项以使用,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我使用System.Linq function Where()来遍历列表

Use this Linq line if you want just the id parts, like "cda6c3f4-8c87-4b12-8f3d-5322ca90eeex"如果您只需要 id 部分,请使用此 Linq 行,例如“cda6c3f4-8c87-4b12-8f3d-5322ca90eeex”

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

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
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用正则表达式从html字符串中提取网址 - How to extract the web address from html string using Regular Expression 如何在.NET中使用正则表达式从字符串中提取子字符串 - How i can extract substring from string using regular expression in .NET 如何使用正则表达式提取字符串中的值 - How to extract values in string by using Regular Expression 使用C#从字符串中提取日期的正则表达式 - Regular Expression to extract date from a string using C# 使用正则表达式从字符串中提取电子邮件地址 - Extract email address(s) from string using regular expression 使用C#中的正则表达式从html字符串中提取句子 - Extract sentence from html string using Regular Expression in C# 如何在C#中使用正则表达式提取字符串 - How to extract string using regular expression in c# 如何从正则表达式中提取字符串并将其转换为时间跨度? - how to extract string from regular expression and convert it to timespan? 如何使用正则表达式从字符串中提取 dd-MMM-yyyy 中的日期 - How to extract date in dd-MMM-yyyy from string using regular expression 如何使用正则表达式提取带引号的字符串? 或者如何在c#中使用这个特定的正则表达式(“[^”] * \\ .csv“)? - How to extract a quoted string using regular expression? OR How to use this particular regular expression (“[^”]*\.csv") in c#?
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM