C# 判断字符串是否可以转为Guid 字符串转Guid

 如何判断一下字符串是不是能转化成guid

/// <summary>
/// 判断字符串是否可以转为Guid
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
public bool IfStr2Guid(string str)
{
    bool IsCan = false;
    str = str.ToLower();//可以不用
    //var regexStr1 = string.Format(@"^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$|^[0-9a-f]{32}$ ");//允许没有'-'
    //var regexStr2 = string.Format(@"^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$ ");
    Match m = Regex.Match(str, @"^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$|^[0-9a-f]{32}$ ", RegexOptions.IgnoreCase);//忽略大小写
    if (m.Success)
    {
        IsCan = true;
    }
    return IsCan;
}

 

posted @ 2021-10-26 10:46  深海地平线  阅读(574)  评论(0编辑  收藏  举报