-3

I have a list of files (some ends with _HHMMss.* where * is the extension).

I want to check if a specific file exist in the list ignoring the "_HHMMss", means: if I have a list:

A_Log_YYYY_MM_DD_121122.csv
B_Log_YYYY_MM_DD_112211.csv
C_Log_YYYY_MM_DD_333333.csv
D_Log_YYYY_MM_DD_555555.csv
E_Log_YYYY_MM_DD_777765.csv

check if "A_Log_YYYY_MM_DD.csv" exist, the answer in this case is TRUE Is there a fast way to do that?

Joseph
  • 1,486
  • 3
  • 21
  • 38

1 Answers1

1
var list = new List<string> { .... }; // add your strings here

Regex reg = new Regex("[A-Z]_Log_[0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{6}.csv");
bool matches = list.Any(x => reg.IsMatch(x));

The linq query (.Any(...)) stops when the first matching element is found.

Flat Eric
  • 7,577
  • 8
  • 32
  • 42