验证给定字符串是否是合法的Guid

    /// <summary>
    
/// 验证给定字符串是否是合法的Guid
    
/// </summary>
    
/// <param name="strToValidate">要验证的字符串</param>
    
/// <returns>true/false</returns>

    
//======== C# ========//
    public static bool IsGuid(string strToValidate)
    {
     
bool isGuid = false;
     
string strRegexPatten = @"^(\{){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}$";
     
if (strToValidate != null && !strToValidate.Equals(""))
     {
      isGuid 
= Regex.IsMatch(strToValidate,strRegexPatten);
     }
     
return isGuid;
    }
'//======== VB ========//
    Public Shared Function IsGuid(ByVal strToValidate As StringAs Boolean
        IsGuid 
= False
        
Dim strRegexPatten As String = "^(\{){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}$"
        
If strToValidate <> Nothing And Not strToValidate.Equals(""Then
            isGuid 
= Regex.IsMatch(strToValidate, strRegexPatten)
        
End If
        
Return isGuid
    
End Function
posted @ 2006-03-02 11:36  blueKnight  Views(1312)  Comments(0Edit  收藏  举报