Nick Larsen
Nick Larsen

Reputation: 18877

Regex file extension filter

I am trying to create a regex that will take all files that do not have a list of extensions. In particular, I am trying to filter out filenames that end in .csv

I have browsed around for an hour and been unable to figure this out. I am using .NET Regex.

Upvotes: 2

Views: 4726

Answers (3)

Austin Salonen
Austin Salonen

Reputation: 50245

If you're using .Net 3.5 or higher, this non-regex solution should work:

var root = new DirectoryInfo(@"C:\");
var files = root.GetFiles();

var filteredFiles = files.Where(f => f.Extension != "csv");

Upvotes: 1

BlackICE
BlackICE

Reputation: 8926

        For Each r As String In _invalidFileExpressions
            If bValid Then
                bValid = Not Regex.IsMatch(FileName, r, RegexOptions.IgnoreCase)
            End If
        Next

r would be defined as .net regular expression, some samples:

        ".*\.csv" 
        ".*d\.txt" 
        "\d\d_\d{8}[dmysDMYS].txt" 
        ".*(batch).*\.txt"
        ".*(elvis).*\.txt"
        ".*(anal).*\.txt"
        ".*(monthlystats)\.txt"
        ".*(rx)\.txt"
        ".*(BAD|bad)\.txt"
        ".*(mm)\.txt"
        ".*(flu)\.txt"

Upvotes: 0

Jason Webb
Jason Webb

Reputation: 8020

The following should do the trick. I just tested it with .net.

^.*\.(?!csv).*$

Be sure to include the IgnoreCase RegexOptions to make it case insensitive.

Upvotes: 5

Related Questions