Roman Cherepanov
Roman Cherepanov

Reputation: 1805

How to filter files with regex by extension but exclude some files?

I want to filter files that have extension .htm, .html, .js, .css, .svg, .png but exclude files that have names index.html, page1.html.

To filter files by extension I use this regex: \.(htm|html|js|css|svg|png)$.

But how can I exclude files with names index.html, page1.html?

Upvotes: 3

Views: 10376

Answers (3)

Phil
Phil

Reputation: 42991

An alternative to using Regex - use Linq. Not so concise but...

var includeExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) 
    {".htm", ".html", ".js", ".css", ".svg", ".png", ".txt"};

var excludeNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase) 
    {"index", "page1"};

var fileNames = Directory.EnumerateFiles("c:\\test")
    .Select(f => new {FileName = f, Ext=Path.GetExtension(f), Name=Path.GetFileNameWithoutExtension(f)})
    .Where(f => includeExtensions.Contains(f.Ext))
    .Where(f => !excludeNames.Contains(f.Name))
    .Select(f => f.FileName);

Upvotes: 1

m87
m87

Reputation: 4523

Perhaps try something like this :

(?<!index|page1)(.html|.htm|.css|.js|.png|.svg)

see demo

Upvotes: 0

falsetru
falsetru

Reputation: 369074

You can use negative look-ahead assertion to exclude files:

^(?!(index|page1)\.html$).*\.(htm|html|js|css|svg|png)$

DEMO

Upvotes: 4

Related Questions