我有一个列表,其中包含一些像这样的字符串和其他数据。
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,// Parent,Children,Siblings和进程信息
List<WindowInformation> windowListExtended = WindowList.GetAllWindowsExtendedInfo();
要匹配的常规表达式是:
HwndWrapper\[App.exe;;.*?\]
现在,对于列表中的每个匹配项。我需要提取匹配的字符串,并使用提取的每个字符串(Foreach或类似的东西)运行一个过程。
请帮忙。
更新:感谢Altaris的帮助,只需要将List转换为string
var message = string.Join(",", windowListExtended);
string pattern = @"HwndWrapper\[LogiOverlay.exe;;.*?]";
MatchCollection matches = Regex.Matches(message, pattern);
据我了解,您想将每个匹配项提取到单独的列表中进行处理,您可以进行以下操作:
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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句