Parse GUID : Guid « Development Class « C# / C Sharp






Parse GUID

 


using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

public class GuidUtil
{
    public static bool GuidTryParse(string s, out Guid result)
    {
        if (s == null)
            throw new ArgumentNullException("s");

        Regex format = new Regex(
            "^[A-Fa-f0-9]{32}$|" +
            "^({|\\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\\))?$|" +
            "^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$");
        Match match = format.Match(s);

        if (match.Success)
        {
            result = new Guid(s);
            return true;
        }
        else
        {
            result = Guid.Empty;
            return false;
        }
    }
}

   
  








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.Convert Guid To Guid Binary String
5.Is valid GUID
6.Use Regular expression to check the Guid