Mega Code Archive

 
Categories / C# / Development Class
 

Use Regular expression to check the Guid

using System; using System.Reflection; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; class GatWebUtility {     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);     public static bool IsGuid(string candidate, out Guid output)     {         bool isValid = false;         output = Guid.Empty;         if (candidate != null)         {             if (isGuid.IsMatch(candidate))             {                 output = new Guid(candidate);                 isValid = true;             }         }         return isValid;     } }