简体   繁体   English

如何 select 匹配多个正则表达式的文件列表?

[英]How to select a list of files which matches multiple Regular Expressions?

I have the code working for a single reg.我的代码适用于单个 reg。 exp check, but now I need to check for another reg. exp检查,但现在我需要检查另一个reg。 expression.表达。

Following is the current code,以下是当前代码,

Regex regex = new Regex(@"\s*(t|T)est\s+(c|C)ommon\s*");

var filelist = from file in Directory.EnumerateFiles(sourceDirectory, "*" + fileExtension, SearchOption.AllDirectories)
                               from line in File.ReadLines(file)
                               .Select(f => regex.Match(f))
                               .Where(m => m.Success)
                               select new
                               {
                                   File = file,
                                   Line = line
                               };

This gives me a list of file paths of files which contains 'Test Common'.这给了我一个包含“Test Common”的文件的文件路径列表。 Now along with this check I need to check for 'Read Me'.现在除了这个检查,我还需要检查“Read Me”。

Basically I need to check if the file contains 'Test Common' and 'Read Me'.基本上我需要检查文件是否包含“Test Common”和“Read Me”。

Updated更新

Regex regex = new Regex(@"^\s*\b(type\s*:\s*test\s+common\b\s*\bowner\s*:\s*read\s+me\s*\b)\b\s*$", RegexOptions.Multiline | RegexOptions.IgnoreCase);
    
    var filelist = from file in Directory.EnumerateFiles(sourceDirectory, "*" + fileExtension, SearchOption.AllDirectories)
                                   from line in File.ReadLines(file)
                                   .Select(f => regex.Match(f))
                                   .Where(m => m.Success)
                                   select new
                                   {
                                       File = file,
                                       Line = line
                                   };
               

But this doesn't give any result.但这并没有给出任何结果。

-----Testing File----- -----测试文件-----

---
type: Test common
owner: Read ME
---

#All Tests

This will call all the Tests

You could use the following regex pattern, which uses an alternation:您可以使用以下正则表达式模式,该模式使用交替:

^.*\b(test common\b.*\bread me\b|\bread me\b.*\btest common)\b.*$

Regex regex = new Regex(@"^.*\b(test common\b.*\bread me\b|\bread me\b.*\btest common)\b.*$",
                        RegexOptions.IgnoreCase);

Another option would be to use two positive lookaheads which assert that each phrase appears:另一种选择是使用两个肯定的前瞻,它们断言每个短语都会出现:

^(?=.*\btest common\b(?=.*\b read me\b).*$

Using a single reg exp didn't work for this code because the reg expressions were in two separate lines not in the same line and that process made it more complex.使用单个 reg exp 不适用于此代码,因为 reg 表达式位于两个单独的行中,而不是在同一行中,并且该过程使其更加复杂。 The easiest solution was to first filter from 1st reg exp, then filtering that result from second reg.最简单的解决方案是首先从第一个 reg exp 过滤,然后从第二个 reg 过滤结果。 exp. exp。 So Finlay I'm left with files where both reg.所以 Finlay 我留下了两个 reg 的文件。 expressions match.表达式匹配。

Regex regex_type = new Regex(@"\s*type\s*:\s*test\s+common\s*", RegexOptions.IgnoreCase);
    Regex regex_owner = new Regex(@"\s*owner\s*:\s*read\s+me\s*", RegexOptions.IgnoreCase);
                

                //Filter from Type
                var filelist = from file in Directory.EnumerateFiles(sourceDirectory, "*" + fileExtension, SearchOption.AllDirectories)
                               from lines in File.ReadLines(file)
                               .Select(lines => regex_type.Match(lines))
                               .Where(m => m.Success)
                               select file;

                //Then filter that from owner
                filelist = from file in filelist
                           from lines in File.ReadLines(file)
                           .Select(lines => regex_owner.Match(lines))
                           .Where(m => m.Success)
                           select file;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM