[英]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.