C# 使用正则表达式列表查找匹配的目录
我有一个IEnumerable,我想使用一个正则表达式数组来筛选它,以找到潜在的匹配项。我一直在尝试使用linq连接我的目录和正则表达式字符串,但似乎无法正确连接。这就是我想做的C# 使用正则表达式列表查找匹配的目录,c#,regex,linq,directoryinfo,C#,Regex,Linq,Directoryinfo,我有一个IEnumerable,我想使用一个正则表达式数组来筛选它,以找到潜在的匹配项。我一直在尝试使用linq连接我的目录和正则表达式字符串,但似乎无法正确连接。这就是我想做的 string[] regexStrings = ... // some regex match code here. // get all of the directories below some root that match my initial criteria. var directories = from
string[] regexStrings = ... // some regex match code here.
// get all of the directories below some root that match my initial criteria.
var directories = from d in root.GetDirectories("*", SearchOption.AllDirectories)
where (d.Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0
&& (d.GetDirectories().Where(dd => (dd.Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0).Count() > 0// has directories
|| d.GetFiles().Where(ff => (ff.Attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0).Count() > 0) // has files)
select d;
// filter the list of all directories based on the strings in the regex array
var filteredDirs = from d in directories
join s in regexStrings on Regex.IsMatch(d.FullName, s) // compiler doesn't like this line
select d;
。。。有什么建议吗?您在加入前缺少了Where关键字您在加入前缺少了Where关键字我不认为您所采取的方法正是您想要的。这将根据所有正则表达式字符串检查名称,而不是在第一次匹配时短路。此外,如果一个匹配多个模式,它将复制目录 我想你想要的是:
string[] regexStrings = ... // some regex match code here.
// get all of the directories below some root that match my initial criteria.
var directories = from d in root.GetDirectories("*", SearchOption.AllDirectories)
where (d.Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0
&& (d.GetDirectories().Where(dd => (dd.Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0).Count() > 0// has directories
|| d.GetFiles().Where(ff => (ff.Attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0).Count() > 0) // has files)
select d;
// filter the list of all directories based on the strings in the regex array
var filteredDirs = directories.Where(d =>
{
foreach (string pattern in regexStrings)
{
if (System.Text.RegularExpressions.Regex.IsMatch(d.FullName, pattern))
{
return true;
}
}
return false;
});
我不认为你采取的方法正是你想要的。这将根据所有正则表达式字符串检查名称,而不是在第一次匹配时短路。此外,如果一个匹配多个模式,它将复制目录 我想你想要的是:
string[] regexStrings = ... // some regex match code here.
// get all of the directories below some root that match my initial criteria.
var directories = from d in root.GetDirectories("*", SearchOption.AllDirectories)
where (d.Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0
&& (d.GetDirectories().Where(dd => (dd.Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0).Count() > 0// has directories
|| d.GetFiles().Where(ff => (ff.Attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0).Count() > 0) // has files)
select d;
// filter the list of all directories based on the strings in the regex array
var filteredDirs = directories.Where(d =>
{
foreach (string pattern in regexStrings)
{
if (System.Text.RegularExpressions.Regex.IsMatch(d.FullName, pattern))
{
return true;
}
}
return false;
});
如果您只需要匹配所有正则表达式的目录
var result = directories
.Where(d => regexStrings.All(s => Regex.IsMatch(d.FullName, s)));
var result = directories
.Where(d => regexStrings.Any(s => Regex.IsMatch(d.FullName, s)));
如果您只希望目录至少匹配一个正则表达式
var result = directories
.Where(d => regexStrings.All(s => Regex.IsMatch(d.FullName, s)));
var result = directories
.Where(d => regexStrings.Any(s => Regex.IsMatch(d.FullName, s)));
如果您只需要匹配所有正则表达式的目录
var result = directories
.Where(d => regexStrings.All(s => Regex.IsMatch(d.FullName, s)));
var result = directories
.Where(d => regexStrings.Any(s => Regex.IsMatch(d.FullName, s)));
如果您只希望目录至少匹配一个正则表达式
var result = directories
.Where(d => regexStrings.All(s => Regex.IsMatch(d.FullName, s)));
var result = directories
.Where(d => regexStrings.Any(s => Regex.IsMatch(d.FullName, s)));
Doh!-我也总是忘记那些(任何&所有的)事情我也总是忘记那些(任何&所有的)。