使用正则表达式列表查找匹配的目录

发布于 2024-07-26 00:16:19 字数 1142 浏览 4 评论 0原文

我有一个 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 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;

关于如何让它发挥作用有什么建议吗?

I have an IEnumerable<DirectoryInfo> that I want to filter down using an array of regular expressions to find the potential matches. I've been trying to join my directory and regex strings using linq, but can't seem to get it right. Here's what I'm trying to do...

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;

... any suggestions on how I can get this to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦中的蝴蝶 2024-08-02 00:16:19

如果您只想匹配所有正则表达式的目录。

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)));

If you only want directories that match all regular expressions.

var result = directories
    .Where(d => regexStrings.All(s => Regex.IsMatch(d.FullName, s)));

If you only want directories that match at least one regular expressions.

var result = directories
    .Where(d => regexStrings.Any(s => Regex.IsMatch(d.FullName, s)));
谁人与我共长歌 2024-08-02 00:16:19

无论如何,我认为您所采取的方法并不完全是您想要的。 这将根据所有正则表达式字符串检查名称,而不是在第一个匹配时短路。 此外,如果一个目录匹配多个模式,它会复制目录。

我想你想要这样的东西:

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;
    });

I don't think that the approach you're taking is exactly what you want anyhow. That will check the name against ALL regex strings, rather than short circuiting on the first match. Additionally, it would duplicate the directories if one matched more than one pattern.

I think you want something like:

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;
    });
゛时过境迁 2024-08-02 00:16:19

您在连接前面缺少Where关键字

you are missing your Where keyword in front of the join

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文