以下是代码经常用到的一些字符串校验的函数
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace Linus.Fin.Public.StringLib
- {
- public class RegularExp
- {
- #region 常用正则表达式枚举类
- public class RegExpPattern
- {
- /// <summary>
- /// 非负整数(正整数 + 0)
- /// </summary>
- public const string NonNegativeNumber = @"^/d+$";
- /// <summary>
- /// 非正整数(负整数 + 0)
- /// </summary>
- public const string NonPositiveNumber = @"^((-/d+)|(0+))$";
- /// <summary>
- /// 正整数
- /// </summary>
- public const string PositiveNumber = @"^[0-9]*[1-9][0-9]*$";
- /// <summary>
- /// 负整数
- /// </summary>
- public const string NegativeNumber = @"^-[0-9]*[1-9][0-9]*$";
- /// <summary>
- /// 整数
- /// </summary>
- public const string Number = @"^-?/d+$";
- /// <summary>
- /// 非负浮点数(正浮点数 + 0)
- /// </summary>
- public const string NonNegativeFloat = @"^/d+(/./d+)?$";
- /// <summary>
- /// 正浮点数
- /// </summary>
- 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]*))$";
- /// <summary>
- /// 非正浮点数(负浮点数 + 0)
- /// </summary>
- public const string NonPositiveFloat = @"^((-/d+(/./d+)?)|(0+(/.0+)?))$";
- /// <summary>
- /// 负浮点数
- /// </summary>
- 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]*)))$";
- /// <summary>
- /// 浮点数
- /// </summary>
- public const string Float = @"^(-?/d+)(/./d+)?$";
- /// <summary>
- /// 由26个英文字母组成的字符串
- /// </summary>
- public const string LetterString = @"^[A-Za-z]+$";
- /// <summary>
- /// 由26个英文字母的大写组成的字符串
- /// </summary>
- public const string UpperLetterString = @"^[A-Z]+$";
- /// <summary>
- /// 由26个英文字母的小写组成的字符串
- /// </summary>
- public const string LowerLetterString = @"^[a-z]+$";
- /// <summary>
- /// 由数字和26个英文字母组成的字符串
- /// </summary>
- public const string DigitLetterString = @"^[A-Za-z0-9]+$";
- /// <summary>
- /// 由数字、26个英文字母或者下划线组成的字符串
- /// </summary>
- public const string GeneralString = @"^/w+$";
- /// <summary>
- /// 电话号码
- /// </summary>
- public const string TelephoneNumber = @"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?";
- /// <summary>
- /// IP地址
- /// </summary>
- 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])$";
- /// <summary>
- /// 匹配中文字符的正则表达式
- /// </summary>
- public const string ChineseString = @"[/u4e00-/u9fa5]";
- /// <summary>
- /// 匹配双字节字符(包括汉字在内)
- /// </summary>
- public const string UnicodeText = @"[^/x00-/xff]";
- /// <summary>
- /// 匹配空行的正则表达式
- /// </summary>
- public const string BlankLine = @"/n[/s| ]*/r";
- /// <summary>
- /// 匹配HTML标记的正则表达式
- /// </summary>
- public const string HtmlFlag = @"/<(.*)>.*<///1>|<(.*) //>/";
- /// <summary>
- /// 匹配Email地址的正则表达式
- /// </summary>
- public const string EmailAddr = @"/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*";
- /// <summary>
- /// 匹配网址URL的正则表达式
- /// </summary>
- public const string UrlAddr = @"^[a-zA-z]+://(//w+(-//w+)*)(//.(//w+(-//w+)*))*(//?//S*)?$";
- /// <summary>
- /// 匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线)
- /// </summary>
- public const string BankAcct = @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
- /// <summary>
- /// 匹配国内电话号码
- /// </summary>
- public const string ChinaTelephone = @"(/d{3}-|/d{4}-)?(/d{8}|/d{7})?";
- }
- #endregion
- #region private function
- private static bool IsMatched(string strInputText, string strRegPatter, RegexOptions regFlag)
- {
- Regex ex = new Regex(strRegPatter, regFlag);
- return ex.IsMatch(strInputText);
- }
- #endregion
- #region public function
- public static bool IsMailAddr(string strInputText)
- {
- return IsMailAddr(strInputText, true);
- }
- public static bool IsMailAddr(string strInputText, bool bIgnoreCase)
- {
- RegexOptions regFlag = RegexOptions.Compiled;
- if (bIgnoreCase) regFlag = regFlag | RegexOptions.IgnoreCase;
- return IsMatched(strInputText, RegExpPattern.EmailAddr, regFlag);
- }
- public static bool IsIPAddr(string strInputText)
- {
- return IsMatched(strInputText, RegExpPattern.IPAddr, RegexOptions.Compiled);
- }
- public static bool IsNumber(string strInputText)
- {
- return IsMatched(strInputText, RegExpPattern.Number, RegexOptions.Compiled);
- }
- public static bool IsNegativeNumber(string strInputText)
- {
- return IsMatched(strInputText, RegExpPattern.NegativeNumber, RegexOptions.Compiled);
- }
- public static bool IsPositiveNumber(string strInputText)
- {
- return IsMatched(strInputText, RegExpPattern.PositiveNumber, RegexOptions.Compiled);
- }
- public static bool IsNonNegativeNumber(string strInputText)
- {
- return IsMatched(strInputText, RegExpPattern.NonNegativeNumber, RegexOptions.Compiled);
- }
- public static bool IsNonPositiveNumber(string strInputText)
- {
- return IsMatched(strInputText, RegExpPattern.NonPositiveNumber, RegexOptions.Compiled);
- }
- public static bool IsFloat(string strInputText)
- {
- return IsMatched(strInputText, RegExpPattern.Float, RegexOptions.Compiled);
- }
- public static bool IsNegativeFloat(string strInputText)
- {
- return IsMatched(strInputText, RegExpPattern.NegativeFloat, RegexOptions.Compiled);
- }
- public static bool IsPositiveFloat(string strInputText)
- {
- return IsMatched(strInputText, RegExpPattern.PositiveFloat, RegexOptions.Compiled);
- }
- public static bool IsNonNegativeFloat(string strInputText)
- {
- return IsMatched(strInputText, RegExpPattern.NonNegativeFloat, RegexOptions.Compiled);
- }
- public static bool IsNonPositiveFloat(string strInputText)
- {
- return IsMatched(strInputText, RegExpPattern.NonPositiveFloat, RegexOptions.Compiled);
- }
- #endregion
- }
- }