分享

C#结合正则表达式判断各种用户输入合法性

 昵称10504424 2013-02-19
  1. using System;  
  2. using System.Text.RegularExpressions;  
  3.   
  4.  namespace SG_VQCDataCollection  
  5. {  
  6.     /// <summary>   
  7.     /// 通过Framwork类库中的Regex类实现了一些特殊功能数据检查   
  8.      /// </summary>   
  9.      public class MetarnetRegex  
  10.     {  
  11.   
  12.          private static MetarnetRegex instance = null;  
  13.          public static MetarnetRegex GetInstance()  
  14.          {  
  15.            if (MetarnetRegex.instance == null)  
  16.              {  
  17.                  MetarnetRegex.instance = new MetarnetRegex();  
  18.              }  
  19.              return MetarnetRegex.instance;  
  20.          }  
  21.          private MetarnetRegex()  
  22.          {  
  23.          }  
  24.   
  25.          /// <summary>   
  26.          /// 判断输入的字符串只包含汉字   
  27.          /// </summary>   
  28.          /// <param name="input"></param>   
  29.          /// <returns></returns>   
  30.          public static bool IsChineseCh(string input)  
  31.          {  
  32.              //Regex regex = new Regex("^[\一-\龥]+$");   
  33.   
  34.             //改了一下   
  35.   
  36.              Regex regex = new Regex("^[\一-\龥]+$");  
  37.              return regex.IsMatch(input);  
  38.          }  
  39.   
  40.          /// <summary>   
  41.          /// 匹配3位或4位区号的电话号码,其中区号可以用小括号括起来,   
  42.          /// 也可以不用,区号与本地号间可以用连字号或空格间隔,   
  43.          /// 也可以没有间隔   
  44.          /// \(0\d{2}\)[- ]?\d{8}|0\d{2}[- ]?\d{8}|\(0\d{3}\)[- ]?\d{7}|0\d{3}[- ]?\d{7}   
  45.          /// </summary>   
  46.          /// <param name="input"></param>   
  47.          /// <returns></returns>   
  48.          public static bool IsPhone(string input)  
  49.          {  
  50.              string pattern = "^\\(0\\d{2}\\)[- ]?\\d{8}$|^0\\d{2}[- ]?\\d{8}$|^\\(0\\d{3}\\)[- ]?\\d{7}$|^0\\d{3}[- ]?\\d{7}$";  
  51.              Regex regex = new Regex(pattern);  
  52.              return regex.IsMatch(input);  
  53.          }  
  54.   
  55.          /// <summary>   
  56.          /// 判断输入的字符串是否是一个合法的手机号   
  57.          /// </summary>   
  58.          /// <param name="input"></param>   
  59.          /// <returns></returns>   
  60.          public static bool IsMobilePhone(string input)  
  61.          {  
  62.              Regex regex = new Regex("^13\\d{9}$");  
  63.              return regex.IsMatch(input);  
  64.   
  65.          }  
  66.   
  67.   
  68.          /// <summary>   
  69.          /// 判断输入的字符串只包含数字   
  70.          /// 可以匹配整数和浮点数   
  71.          /// ^-?\d+$|^(-?\d+)(\.\d+)?$   
  72.          /// </summary>   
  73.          /// <param name="input"></param>   
  74.          /// <returns></returns>   
  75.          public static bool IsNumber(string input)  
  76.          {  
  77.              string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";  
  78.              Regex regex = new Regex(pattern);  
  79.              return regex.IsMatch(input);  
  80.          }  
  81.   
  82.          /// <summary>   
  83.          /// 匹配非负整数   
  84.          ///   
  85.          /// </summary>   
  86.          /// <param name="input"></param>   
  87.          /// <returns></returns>   
  88.          public static bool IsNotNagtive(string input)  
  89.          {  
  90.              Regex regex = new Regex(@"^\d+$");  
  91.              return regex.IsMatch(input);  
  92.          }  
  93.          /// <summary>   
  94.          /// 匹配正整数   
  95.          /// </summary>   
  96.          /// <param name="input"></param>   
  97.          /// <returns></returns>   
  98.          public static bool IsUint(string input)  
  99.          {  
  100.              Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");  
  101.              return regex.IsMatch(input);  
  102.          }  
  103.          /// <summary>   
  104.          /// 判断输入的字符串字包含英文字母   
  105.          /// </summary>   
  106.          /// <param name="input"></param>   
  107.          /// <returns></returns>   
  108.          public static bool IsEnglisCh(string input)  
  109.          {  
  110.              Regex regex = new Regex("^[A-Za-z]+$");  
  111.              return regex.IsMatch(input);  
  112.          }  
  113.   
  114.   
  115.          /// <summary>   
  116.          /// 判断输入的字符串是否是一个合法的Email地址   
  117.          /// </summary>   
  118.          /// <param name="input"></param>   
  119.          /// <returns></returns>   
  120.          public static bool IsEmail(string input)  
  121.          {  
  122.              string pattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";  
  123.              Regex regex = new Regex(pattern);  
  124.              return regex.IsMatch(input);  
  125.          }  
  126.   
  127.   
  128.          /// <summary>   
  129.          /// 判断输入的字符串是否只包含数字和英文字母   
  130.          /// </summary>   
  131.          /// <param name="input"></param>   
  132.          /// <returns></returns>   
  133.          public static bool IsNumAndEnCh(string input)  
  134.          {  
  135.              string pattern = @"^[A-Za-z0-9]+$";  
  136.              Regex regex = new Regex(pattern);  
  137.              return regex.IsMatch(input);  
  138.          }  
  139.   
  140.   
  141.          /// <summary>   
  142.          /// 判断输入的字符串是否是一个超链接   
  143.          /// </summary>   
  144.          /// <param name="input"></param>   
  145.          /// <returns></returns>   
  146.          public static bool IsURL(string input)  
  147.          {  
  148.              //string pattern = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";   
  149.              string pattern = @"^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$";  
  150.              Regex regex = new Regex(pattern);  
  151.              return regex.IsMatch(input);  
  152.          }  
  153.   
  154.   
  155.          /// <summary>   
  156.          /// 判断输入的字符串是否是表示一个IP地址   
  157.          /// </summary>   
  158.          /// <param name="input">被比较的字符串</param>   
  159.          /// <returns>是IP地址则为True</returns>   
  160.          public static bool IsIPv4(string input)  
  161.          {  
  162.   
  163.              string[] IPs = input.Split('.');  
  164.              Regex regex = new Regex(@"^\d+$");  
  165.              for (int i = 0; i < IPs.Length; i++)  
  166.              {  
  167.                  if (!regex.IsMatch(IPs[i]))  
  168.                  {  
  169.                      return false;  
  170.                  }  
  171.                  if (Convert.ToUInt16(IPs[i]) > 255)  
  172.                 {  
  173.                      return false;  
  174.                  }  
  175.              }  
  176.              return true;  
  177.          }  
  178.   
  179.   
  180.          /// <summary>   
  181.          /// 计算字符串的字符长度,一个汉字字符将被计算为两个字符   
  182.          /// </summary>   
  183.          /// <param name="input">需要计算的字符串</param>   
  184.          /// <returns>返回字符串的长度</returns>   
  185.          public static int GetCount(string input)  
  186.          {  
  187.              return Regex.Replace(input, @"[\一-\龥/g]""aa").Length;  
  188.          }  
  189.   
  190.          /// <summary>   
  191.          /// 调用Regex中IsMatch函数实现一般的正则表达式匹配   
  192.          /// </summary>   
  193.          /// <param name="pattern">要匹配的正则表达式模式。</param>   
  194.          /// <param name="input">要搜索匹配项的字符串</param>   
  195.          /// <returns>如果正则表达式找到匹配项,则为 true;否则,为 false。</returns>   
  196.          public static bool IsMatch(string pattern, string input)  
  197.          {  
  198.              Regex regex = new Regex(pattern);  
  199.              return regex.IsMatch(input);  
  200.          }  
  201.   
  202.          /// <summary>   
  203.          /// 从输入字符串中的第一个字符开始,用替换字符串替换指定的正则表达式模式的所有匹配项。   
  204.          /// </summary>   
  205.          /// <param name="pattern">模式字符串</param>   
  206.          /// <param name="input">输入字符串</param>   
  207.          /// <param name="replacement">用于替换的字符串</param>   
  208.          /// <returns>返回被替换后的结果</returns>   
  209.          public static string Replace(string pattern, string input, string replacement)  
  210.          {  
  211.              Regex regex = new Regex(pattern);  
  212.              return regex.Replace(input, replacement);  
  213.          }  
  214.   
  215.          /// <summary>   
  216.          /// 在由正则表达式模式定义的位置拆分输入字符串。   
  217.          /// </summary>   
  218.          /// <param name="pattern">模式字符串</param>   
  219.          /// <param name="input">输入字符串</param>   
  220.          /// <returns></returns>   
  221.          public static string[] Split(string pattern, string input)  
  222.          {  
  223.              Regex regex = new Regex(pattern);  
  224.              return regex.Split(input);  
  225.          }  
  226.   
  227.          /// <summary>   
  228.          /// 判断输入的字符串是否是合法的IPV6 地址   
  229.          /// </summary>   
  230.          /// <param name="input"></param>   
  231.          /// <returns></returns>   
  232.          public static bool IsIPV6(string input)  
  233.          {  
  234.              string pattern = "";  
  235.              string temp = input;  
  236.              string[] strs = temp.Split(':');  
  237.              if (strs.Length > 8)  
  238.              {  
  239.                  return false;  
  240.              }  
  241.              int count = MetarnetRegex.GetStringCount(input, "::");  
  242.              if (count > 1)  
  243.              {  
  244.                  return false;  
  245.              }  
  246.              else if (count == 0)  
  247.              {  
  248.                  pattern = @"^([\da-f]{1,4}:){7}[\da-f]{1,4}$";  
  249.   
  250.                  Regex regex = new Regex(pattern);  
  251.                  return regex.IsMatch(input);  
  252.              }  
  253.              else  
  254.              {  
  255.                  pattern = @"^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$";  
  256.                  Regex regex1 = new Regex(pattern);  
  257.                  return regex1.IsMatch(input);  
  258.              }  
  259.   
  260.          }  
  261.          /* ******************************************************************* 
  262.          * 1、通过“:”来分割字符串看得到的字符串数组长度是否小于等于8 
  263.          * 2、判断输入的IPV6字符串中是否有“::”。 
  264.          * 3、如果没有“::”采用 ^([\da-f]{1,4}:){7}[\da-f]{1,4}$ 来判断 
  265.          * 4、如果有“::” ,判断"::"是否止出现一次 
  266.          * 5、如果出现一次以上 返回false 
  267.          * 6、^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$ 
  268.          * ******************************************************************/  
  269.          /// <summary>   
  270.          /// 判断字符串compare 在 input字符串中出现的次数   
  271.          /// </summary>   
  272.          /// <param name="input">源字符串</param>   
  273.          /// <param name="compare">用于比较的字符串</param>   
  274.          /// <returns>字符串compare 在 input字符串中出现的次数</returns>   
  275.          private static int GetStringCount(string input, string compare)  
  276.          {  
  277.              int index = input.IndexOf(compare);  
  278.              if (index != -1)  
  279.              {  
  280.                 return 1 + GetStringCount(input.Substring(index + compare.Length), compare);  
  281.              }  
  282.              else  
  283.              {  
  284.                  return 0;  
  285.              }  
  286.   
  287.          }  
  288.      }  
  289.  }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多