1

I need to filter a list of files. Some of these are csv files, and of those, some of them are appended with a control tag, ".cntl".

Example: file1.csv, file1.csv.cntl

I would like to set up a regex that checks to see if a file contains "csv" and NOT "cntl". Right now I've got this.

csv(?!cntl)

This is not working. What would a proper regex be?

PS. This is all done in C#.

Nick Vaccaro
  • 5,428
  • 6
  • 38
  • 60

3 Answers3

9

Your regex should check to see if a file ends with .csv

\.csv$

Remember that csv could be contained elsewhere in the file name.

Seattle Leonard
  • 6,548
  • 3
  • 27
  • 37
7

The following is the pattern you want.

@"csv(?!\.cntl)"

But, wouldn't it be easier to check:

if (string.EndsWith("cntl"))

Using a regex is unnecessarily complicated.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
0

This should work:

csv(?!.*cntl)
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • If you have not "." will it be possible to have a file that is not a "csv" real file. "Myfilecsv" will to go to true – eriksv88 Dec 09 '10 at 20:39
  • @sv88erik: Yes, but that's not the question the OP asked. – cdhowie Dec 09 '10 at 20:39
  • 1
    "Example: file1.csv, file1.csv.cntl" : as shown in this syntax :) – eriksv88 Dec 09 '10 at 20:41
  • *"I would like to set up a regex that checks to see if a file contains 'csv' and NOT 'cntl'."* – cdhowie Dec 09 '10 at 20:42
  • And I reckon that this is the fault of the OP, then eksplene and file validation of file extensions. But of course you have a point that it has two different answers if you choose to interpret it as you do – eriksv88 Dec 09 '10 at 20:53