0

I'm reading in a list of filenames from our database, and any filename that contains a doesn't contain a guid is considered to be a file that was included as part of a template. The list of files can contain files where some of them have a guid (part of template) and others don't have a guid (not from template). How can I discriminate between the files that have a guid from those that do not have a guid?

Here's a example:

List<string> spotFiles = DAL.HtmlSpot.GetSpotMedia(); //Returns {"manifest.xml", "attributes-97c23e02-e216-431b-9b6b-c5852962e92d.png"}

foreach(string file in spotFiles)
{
    //If file contains a guid as a substring
        //Handle template file
    //Else
        //Handle non-template file
}
Imbajoe
  • 310
  • 3
  • 16

1 Answers1

1

You can do it with Regex like so :

List<string> spotFiles = DAL.HtmlSpot.GetSpotMedia(); //Returns {"manifest.xml", "attributes-97c23e02-e216-431b-9b6b-c5852962e92d.png"}

foreach(string fileName in spotFiles)
{

var guidMatch = Regex.Match(fileName, @"(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}",
        RegexOptions.IgnoreCase);

    if (guidMatch.Success)
    {
        //Handle template file
    }
    else
    {
        //Handle non-template file
    }
}

BUT

  1. Regex performance can be quite expensive
  2. No confidance that the regex will catch 100% of the cases.

If the filenames you dealing with also have some sort of separation say "_" e.g "aaa_bbb_GUID_ccc.txt"
you can split the filename string to parts and then use Guid.TryParse() on each part.

igorc
  • 2,024
  • 2
  • 17
  • 29
  • Thank you. I went with the Regex solution since my case will not be run often enough to be a performance concern, but the method to separate based on delimiters is something I will keep in mind for the future. – Imbajoe Jun 15 '17 at 16:59