public static List<String> StringToIPList(string strIPs) { var reg1 = new Regex(@"(((2[0-4]d)|(25[0-5])|([0-1]?d{1,2})).?){4}(-(((2[0-4]d)|(25[0-5])|([0-1]?d{1,2})).?){4})"); var reg2 = new Regex(@"^(((2[0-4]d)|(25[0-5])|([0-1]?d{1,2})).?){4}$"); if (string.IsNullOrEmpty(strIPs)) { return new List<string>(); } var ips = strIPs.Split(','); var ipList = new List<string>(); var tempList = new List<string>(); object a = null; foreach (var item in ips) { if (reg1.IsMatch(item)) { tempList.Add(item); } if (reg2.IsMatch(item)) { a = reg2.Matches(item); ipList.Add(item); } } var ipListList = new List<String>(); foreach (var item in tempList) { var tempIps = item.Split('-'); var tempResult = true; foreach (var regItem in tempIps) { tempResult &= reg2.IsMatch(regItem); } if (!tempResult) continue; var list = new List<List<int>>(); foreach (var loopItem in tempIps) { var nums = loopItem.Split('.'); if (nums.Length != 4) continue; var listPart = new List<int>(); foreach (var num in nums) { listPart.Add(int.Parse(num)); } list.Add(listPart); } if (list.Count > 2) continue; var listRealPartIps = new List<String>(); for (int i = 0; i < 4; i++) { var tempPartList = new List<String>(); if (list[0][i] > list[1][i]) continue; if (list[0][i] == list[1][i]) { if (i == 0) { tempPartList.Add(list[0][i].ToString()); } else { foreach (var leftNums in listRealPartIps) { tempPartList.Add(leftNums + "." + (list[0][i]).ToString()); } } } else { var ipCount = list[1][i] - list[0][i] + 1; for (int k = 0; k < ipCount; k++) { if (i == 0) { tempPartList.Add((list[0][i] + k).ToString()); } else { foreach (var leftNums in listRealPartIps) { tempPartList.Add(leftNums + "." + (list[0][i] + k).ToString()); } } } } listRealPartIps = tempPartList; } ipList.AddRange(listRealPartIps); } return ipList; }