tags:

views:

472

answers:

4

I have an application which 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?

+2  A: 

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$
Lasse V. Karlsen
+5  A: 

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.

Bart Kiers
+1  A: 
    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
xcud
A: 

RegexBuddy is an excellent way to spend a few bucks (if you have some to spend). It will help you develop, test and debug your regexes. It even creates code snippets for you.

RegexMagic (from the same author) might even help you more: It helps you create a regex pattern from samples. (I haven't tried it though so I can't say if it's good).

Serge - appTranslator