I have an application whic开发者_运维百科h needs to find and then process files which follow a very specific naming convention as follows.
IABC_12345-0_YYYYMMDD_YYYYMMDD_HHMMSS.zip
I cant see any easy way of doing this using a search pattern so Im assuming Ill have to do something like this after I have generated a list of files using a simpler wildcard pattern.
RegEx re = new RegEx("blah");
foreach(FileInfo fi in Directory.GetFiles(path, "I*.zip"))
{
if(re.IsMatch(fi.Name))
//blah blah blah
}
Is this the best way of doing this, and if so, how would I form a regular expression to match this file format?
string pattern = @"I[A-Z]{3}_\d{5}-\d_\d{8}_\d{8}_\d{6}\.zip";
var matches = Directory.GetFiles(@"c:\temp")
.Where(path => Regex.Match(path, pattern).Success);
foreach (string file in matches)
Console.WriteLine(file); // do something
It depends on how specific you want to match those names. Is this specific enough:
I[A-Z]{3}_\d{5}-\d_\d{8}_\d{8}_\d{6}\.zip
?
Explanation:
I // match an 'I'
[A-Z]{3} // followed by three upper case letters
_ // followed by an underscore
\d{5} // followed by five digits
- // followed by a hyphen
\d // followed by a single digit
_ // followed by an underscore
\d{8} // followed by eight digits
_ // followed by an underscore
\d{8} // followed by eight digits
_ // followed by an underscore
\d{6} // followed by six digits
\.zip // followed by '.zip'
But, if you have files whose names contain invalid dates or times, it cannot practically be done with regex alone, especially if your DATE_DATE
part specifies a date range. You will have to match all file names like I (and others) have shown you, and then perform some "regular" programming logic to filter out the invalid ones.
For a simple regular expression that will also match invalid time specifications (ie. hours=73 etc.), you could use something like this:
^I[A-Z]{3}_\d{5}-\d_\d{8}_\d{8}_\d{6}\.zip$
精彩评论