Use Regular expression to check the Guid : Guid « Development Class « C# / C Sharp






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;
    }

}

   
  








Related examples in the same category

1.Create Guid
2.Converts the string representation of a Guid toits Guid equivalent.
3.This code example demonstrates the Guid.NewGuid() method.
4.Parse GUID
5.Convert Guid To Guid Binary String
6.Is valid GUID