Skip to content

Instantly share code, notes, and snippets.

@anotherlab
Created March 11, 2014 03:51
Show Gist options
  • Save anotherlab/9479197 to your computer and use it in GitHub Desktop.
Save anotherlab/9479197 to your computer and use it in GitHub Desktop.
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