Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
using System;
using System.Text.RegularExpressions;
public class Program
{
public void Main()
{
string[] guids = {
"0C885DD3-7DD9-484B-9B20-3E6552BCA144",
"{0C885DD3-7DD9-484B-9B20-3E6552BCA144}",
"0C885DD37DD9484B9B203E6552BCA144",
"{0C885DD3-7DD9-484B-9B20-1E6552BCA144}",
"0C885DD3-7DD9484B9B203E6552BCA144",
"0C885DD37DD9484B9B203E6552BCA14"
};
foreach(var guid in guids)
TestGuid(guid);
}
public void TestGuid(string guid)
{
Console.WriteLine(
String.Format("{0} : {1}, {2}",
guid,
guid.IsGuid(),
guid.IsGuidRegEx()));
}
}
public static class StringHelper
{
public static bool IsGuid(this string guid)
{
Guid newGuid;
return Guid.TryParse(guid, out newGuid);
}
public static bool IsGuidRegEx(this string guid)
{
if (guid != null)
{
Regex guidRegEx =
new Regex(@"^[{]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[}]?$");
return guidRegEx.IsMatch(guid);
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment