ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

IsGuid

Validate if a String contains a GUID in groups of 8, 4, 4, 4, and 12 digits with hyphens between the groups. The entire GUID can optionally be enclosed in matching braces or parentheses.

Source

public static bool IsGuid(this string value)
{
	if (string.IsNullOrEmpty(value))
	{
		return false;
	}

	const string pattern = @"^(\{{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})$";
	return Regex.IsMatch(value, pattern);
}

Example

string testValue = "{CA761232-ED42-11CE-BACD-00AA0057B223}";
bool isGuid = testValue.IsGuid();

Author: Alvaro Torres Tatis

Submitted on: 29 feb. 2012

Language: C#

Type: System.String

Views: 6038