I recently needed to validate a GUID in C# and found this article on how to do it. I've simplified it to make it suit the problem I was trying to solve. Here's what I did with it:

private static Regex isGuid = new Regex(@"^(\{){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.Compiled);

private static bool IsGuid(string candidate)
{
   if (candidate != null)
   {
      if (isGuid.IsMatch(candidate))
      {
         return true;
      }
   }

   return false;
}

This method will validate any string and return true if it is a GUID.

Comments


Comments are closed