现在的位置: 首页 > 综合 > 正文

Using Regular Expression to validate a Guid

2011年08月16日 ⁄ 综合 ⁄ 共 878字 ⁄ 字号 评论关闭
     In this implementationtime,I just show you a simple way to validate a Guid.
1/// <summary>
2/// 验证给定字符串是否是合法的Guid
3/// </summary>
4/// <param name="strToValidate">要验证的字符串</param>
5/// <returns>true/false</returns>
6public static bool IsGuid(string strToValidate)
7{
8 bool isGuid = false;
9 string strRegexPatten = @"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\"
10         +@"-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$";
11 if (strToValidate != null && !strToValidate.Equals(""))
12 {
13  isGuid = Regex.IsMatch(strToValidate,strRegexPatten);
14 }
15 return isGuid;
16}

抱歉!评论已关闭.