【C#】常用正则表达式类

以下是代码经常用到的一些字符串校验的函数
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace Linus.Fin.Public.StringLib
  7. {
  8.     public class RegularExp
  9.     {
  10.         #region 常用正则表达式枚举类
  11.         public class RegExpPattern
  12.         {
  13.             /// <summary>
  14.             /// 非负整数(正整数 + 0) 
  15.             /// </summary>
  16.             public const string NonNegativeNumber = @"^\d+$";
  17.             /// <summary>
  18.             /// 非正整数(负整数 + 0) 
  19.             /// </summary>
  20.             public const string NonPositiveNumber = @"^((-\d+)|(0+))$";
  21.             /// <summary>
  22.             /// 正整数   
  23.             /// </summary>
  24.             public const string PositiveNumber = @"^[0-9]*[1-9][0-9]*$";
  25.             /// <summary>
  26.             /// 负整数 
  27.             /// </summary>
  28.             public const string NegativeNumber = @"^-[0-9]*[1-9][0-9]*$";
  29.             /// <summary>
  30.             /// 整数 
  31.             /// </summary>
  32.             public const string Number = @"^-?\d+$";
  33.             /// <summary>
  34.             /// 非负浮点数(正浮点数 + 0) 
  35.             /// </summary>
  36.             public const string NonNegativeFloat = @"^\d+(\.\d+)?$";
  37.             /// <summary>
  38.             /// 正浮点数 
  39.             /// </summary>
  40.             public const string PositiveFloat = @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
  41.             /// <summary>
  42.             /// 非正浮点数(负浮点数 + 0) 
  43.             /// </summary>
  44.             public const string NonPositiveFloat = @"^((-\d+(\.\d+)?)|(0+(\.0+)?))$";
  45.             /// <summary>
  46.             /// 负浮点数 
  47.             /// </summary>
  48.             public const string NegativeFloat = @"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$";
  49.             /// <summary>
  50.             /// 浮点数 
  51.             /// </summary>
  52.             public const string Float = @"^(-?\d+)(\.\d+)?$";
  53.             /// <summary>
  54.             /// 由26个英文字母组成的字符串 
  55.             /// </summary>
  56.             public const string LetterString = @"^[A-Za-z]+$";
  57.             /// <summary>
  58.             /// 由26个英文字母的大写组成的字符串 
  59.             /// </summary>
  60.             public const string UpperLetterString = @"^[A-Z]+$";
  61.             /// <summary>
  62.             /// 由26个英文字母的小写组成的字符串 
  63.             /// </summary>
  64.             public const string LowerLetterString = @"^[a-z]+$";
  65.             /// <summary>
  66.             /// 由数字和26个英文字母组成的字符串 
  67.             /// </summary>
  68.             public const string DigitLetterString = @"^[A-Za-z0-9]+$";
  69.             /// <summary>
  70.             /// 由数字、26个英文字母或者下划线组成的字符串
  71.             /// </summary>
  72.             public const string GeneralString = @"^\w+$";
  73.             /// <summary>
  74.             /// 电话号码
  75.             /// </summary>
  76.             public const string TelephoneNumber = @"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?";
  77.             /// <summary>
  78.             /// IP地址
  79.             /// </summary>
  80.             public const string IPAddr = @"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$";
  81.             /// <summary>
  82.             /// 匹配中文字符的正则表达式
  83.             /// </summary>
  84.             public const string ChineseString = @"[\u4e00-\u9fa5]";
  85.             /// <summary>
  86.             /// 匹配双字节字符(包括汉字在内)
  87.             /// </summary>
  88.             public const string UnicodeText = @"[^\x00-\xff]";
  89.             /// <summary>
  90.             /// 匹配空行的正则表达式
  91.             /// </summary>
  92.             public const string BlankLine = @"\n[\s| ]*\r";
  93.             /// <summary>
  94.             /// 匹配HTML标记的正则表达式
  95.             /// </summary>
  96.             public const string HtmlFlag = @"/<(.*)>.*<\/\1>|<(.*) \/>/";
  97.             /// <summary>
  98.             /// 匹配Email地址的正则表达式
  99.             /// </summary>
  100.             public const string EmailAddr = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
  101.             /// <summary>
  102.             /// 匹配网址URL的正则表达式
  103.             /// </summary>
  104.             public const string UrlAddr = @"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";
  105.             /// <summary>
  106.             /// 匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线)
  107.             /// </summary>
  108.             public const string BankAcct = @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
  109.             /// <summary>
  110.             /// 匹配国内电话号码
  111.             /// </summary>
  112.             public const string ChinaTelephone = @"(\d{3}-|\d{4}-)?(\d{8}|\d{7})?";
  113.         } 
  114.         #endregion
  115.         #region private function
  116.         private static bool IsMatched(string strInputText, string strRegPatter, RegexOptions regFlag)
  117.         {
  118.             Regex ex = new Regex(strRegPatter, regFlag);
  119.             return ex.IsMatch(strInputText);
  120.         } 
  121.         #endregion
  122.         #region public function
  123.         public static bool IsMailAddr(string strInputText)
  124.         {
  125.             return IsMailAddr(strInputText, true);
  126.         }
  127.         public static bool IsMailAddr(string strInputText, bool bIgnoreCase)
  128.         {
  129.             RegexOptions regFlag = RegexOptions.Compiled;
  130.             if (bIgnoreCase) regFlag = regFlag | RegexOptions.IgnoreCase;
  131.             return IsMatched(strInputText, RegExpPattern.EmailAddr, regFlag);
  132.         }
  133.         public static bool IsIPAddr(string strInputText)
  134.         {
  135.             return IsMatched(strInputText, RegExpPattern.IPAddr, RegexOptions.Compiled);
  136.         }
  137.         public static bool IsNumber(string strInputText)
  138.         {
  139.             return IsMatched(strInputText, RegExpPattern.Number, RegexOptions.Compiled);
  140.         }
  141.         public static bool IsNegativeNumber(string strInputText)
  142.         {
  143.             return IsMatched(strInputText, RegExpPattern.NegativeNumber, RegexOptions.Compiled);
  144.         }
  145.         public static bool IsPositiveNumber(string strInputText)
  146.         {
  147.             return IsMatched(strInputText, RegExpPattern.PositiveNumber, RegexOptions.Compiled);
  148.         }
  149.         public static bool IsNonNegativeNumber(string strInputText)
  150.         {
  151.             return IsMatched(strInputText, RegExpPattern.NonNegativeNumber, RegexOptions.Compiled);
  152.         }
  153.         public static bool IsNonPositiveNumber(string strInputText)
  154.         {
  155.             return IsMatched(strInputText, RegExpPattern.NonPositiveNumber, RegexOptions.Compiled);
  156.         }
  157.         public static bool IsFloat(string strInputText)
  158.         {
  159.             return IsMatched(strInputText, RegExpPattern.Float, RegexOptions.Compiled);
  160.         }
  161.         public static bool IsNegativeFloat(string strInputText)
  162.         {
  163.             return IsMatched(strInputText, RegExpPattern.NegativeFloat, RegexOptions.Compiled);
  164.         }
  165.         public static bool IsPositiveFloat(string strInputText)
  166.         {
  167.             return IsMatched(strInputText, RegExpPattern.PositiveFloat, RegexOptions.Compiled);
  168.         }
  169.         public static bool IsNonNegativeFloat(string strInputText)
  170.         {
  171.             return IsMatched(strInputText, RegExpPattern.NonNegativeFloat, RegexOptions.Compiled);
  172.         }
  173.         public static bool IsNonPositiveFloat(string strInputText)
  174.         {
  175.             return IsMatched(strInputText, RegExpPattern.NonPositiveFloat, RegexOptions.Compiled);            
  176.         } 
  177.         
  178.         #endregion
  179.     }
  180. }
posted @ 2008-11-03 21:28  linus_wang  阅读(263)  评论(0编辑  收藏  举报