C# 判斷輸入的字符串是否是合法的IPV6 地址

/// <summary>From:www.uzhanbao.com
         /// 判斷輸入的字符串是否是合法的IPV6 地址
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public static bool IsIPV6(string input)
         {
             string pattern = "";
             string temp = input;
             string[] strs = temp.Split(':');
             if (strs.Length > 8)
             {
                 return false;
             }
             int count = MetarnetRegex.GetStringCount(input, "::");
             if (count > 1)
             {
                 return false;
             }
             else if (count == 0)
             {
                 pattern = @"^([\da-f]{1,4}:){7}[\da-f]{1,4}$";
 
                 Regex regex = new Regex(pattern);
                 return regex.IsMatch(input);
             }
             else
             {
                 pattern = @"^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$";
                 Regex regex1 = new Regex(pattern);
                 return regex1.IsMatch(input);
             }
 
         }
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章