Code-Validator:验证IPv6地址
Posted storebook
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Code-Validator:验证IPv6地址相关的知识,希望对你有一定的参考价值。
ylbtech-Code-Validator:验证IPv6地址 |
1.返回顶部 |
1、
using System; using System.Text.RegularExpressions; namespace Sp.Common { public class ValidatorHelper { /// <summary> /// 验证IPv6地址 /// [可用于匹配任何一个合法的IPv6地址] /// </summary> /// <param name="input">待验证的字符串</param> /// <returns>是否匹配</returns> public static bool IsIPv6(string input) { string pattern = @"^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*$"; return IsMatch(input, pattern); } #region 匹配方法 /// <summary> /// 验证字符串是否匹配正则表达式描述的规则 /// </summary> /// <param name="inputStr">待验证的字符串</param> /// <param name="patternStr">正则表达式字符串</param> /// <returns>是否匹配</returns> public static bool IsMatch(string inputStr, string patternStr) { return IsMatch(inputStr, patternStr, false, false); } /// <summary> /// 验证字符串是否匹配正则表达式描述的规则 /// </summary> /// <param name="inputStr">待验证的字符串</param> /// <param name="patternStr">正则表达式字符串</param> /// <param name="ifIgnoreCase">匹配时是否不区分大小写</param> /// <returns>是否匹配</returns> public static bool IsMatch(string inputStr, string patternStr, bool ifIgnoreCase) { return IsMatch(inputStr, patternStr, ifIgnoreCase, false); } /// <summary> /// 验证字符串是否匹配正则表达式描述的规则 /// </summary> /// <param name="inputStr">待验证的字符串</param> /// <param name="patternStr">正则表达式字符串</param> /// <param name="ifValidateWhiteSpace">是否验证空白字符串</param> /// <returns>是否匹配</returns> public static bool IsMatch1(string inputStr, string patternStr, bool ifValidateWhiteSpace) { return IsMatch(inputStr, patternStr, false, ifValidateWhiteSpace); } /// <summary> /// 验证字符串是否匹配正则表达式描述的规则 /// </summary> /// <param name="inputStr">待验证的字符串</param> /// <param name="patternStr">正则表达式字符串</param> /// <param name="ifIgnoreCase">匹配时是否不区分大小写</param> /// <param name="ifValidateWhiteSpace">是否验证空白字符串</param> /// <returns>是否匹配</returns> public static bool IsMatch(string inputStr, string patternStr, bool ifIgnoreCase, bool ifValidateWhiteSpace) { if (!ifValidateWhiteSpace && string.IsNullOrEmpty(inputStr)) return false;//如果不要求验证空白字符串而此时传入的待验证字符串为空白字符串,则不匹配 Regex regex = null; if (ifIgnoreCase) regex = new Regex(patternStr, RegexOptions.IgnoreCase);//指定不区分大小写的匹配 else regex = new Regex(patternStr); return regex.IsMatch(inputStr); } #endregion } }
2、
2.返回顶部 |
3.返回顶部 |
4.返回顶部 |
5.返回顶部 |
6.返回顶部 |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
以上是关于Code-Validator:验证IPv6地址的主要内容,如果未能解决你的问题,请参考以下文章