C#常用的正则表达式

正则表达式用于字符串处理、表单验证等场合,实用高效。现将一些常用的表达式收集于此,以备不时之需。

匹配中文字符的正则表达式: ["u4e00-"u9fa5]
评注:匹配中文还真是个头疼的事,有了这个表达式就好办了

匹配双字节字符(包括汉字在内):[^"x00-"xff]
评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)

匹配空白行的正则表达式:"n"s*"r
评注:可以用来删除空白行

匹配HTML标记的正则表达式:<("S*?)[^>]*>.*?</"1>|<.*? />
评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力

匹配首尾空白字符的正则表达式:^"s*|"s*$
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式

匹配Email地址的正则表达式:"w+([-+.]"w+)*@"w+([-.]"w+)*"."w+([-.]"w+)*
评注:表单验证时很实用

匹配网址URL的正则表达式:[a-zA-z]+://[^"s]*
评注:网上流传的版本功能很有限,上面这个基本可以满足需求

匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
评注:表单验证时很实用

匹配国内电话号码:"d{3}-"d{8}|"d{4}-"d{7}
评注:匹配形式如 0511-4405222 或 021-87888822

匹配腾讯QQ号:[1-9][0-9]{4,}
评注:腾讯QQ号从10000开始

匹配中国邮政编码:[1-9]"d{5}(?!"d)
评注:中国邮政编码为6位数字

匹配身份证:"d{15}|"d{18}
评注:中国的身份证为15位或18位

匹配ip地址:"d+"."d+"."d+"."d+
评注:提取ip地址时有用

匹配特定数字:
^[1-9]"d*$    //匹配正整数
^-[1-9]"d*$   //匹配负整数
^-?[1-9]"d*$   //匹配整数
^[1-9]"d*|0$  //匹配非负整数(正整数 + 0)
^-[1-9]"d*|0$   //匹配非正整数(负整数 + 0)
^[1-9]"d*"."d*|0"."d*[1-9]"d*$   //匹配正浮点数
^-([1-9]"d*"."d*|0"."d*[1-9]"d*)$  //匹配负浮点数
^-?([1-9]"d*"."d*|0"."d*[1-9]"d*|0?".0+|0)$  //匹配浮点数
^[1-9]"d*"."d*|0"."d*[1-9]"d*|0?".0+|0$   //匹配非负浮点数(正浮点数 + 0)
^(-([1-9]"d*"."d*|0"."d*[1-9]"d*))|0?".0+|0$  //匹配非正浮点数(负浮点数 + 0)
评注:处理大量数据时有用,具体应用时注意修正

匹配特定字符串:
^[A-Za-z]+$  //匹配由26个英文字母组成的字符串
^[A-Z]+$  //匹配由26个英文字母的大写组成的字符串
^[a-z]+$  //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$  //匹配由数字和26个英文字母组成的字符串
^"w+$  //匹配由数字、26个英文字母或者下划线组成的字符串
评注:最基本也是最常用的一些表达式

下面是我项目中一个正则表达式类

ContractedBlock.gif ExpandedBlockStart.gif
  1public class Common
  2ExpandedBlockStart.gifContractedBlock.gif{
  3    //private const string REG_DATE   = @"^("d{2}|"d{4})["-"/]((0?[1-9])|(1[0-2]))["-"/]((0?[1-9])|((1|2)[0-9])|30|31)$"; 
  4    private const string REG_DATE = @"^("d{2}|"d{4})((0[1-9])|(1[0-2]))((0[1-9])|((1|2)[0-9])|30|31)$";
  5    private const string REG_PHONE = @"^((0[0-9]{2,3}){0,1}([0-9]{7,8}))$";
  6    private const string REG_EMAIL = @"^(["w-".]+)@(("[[0-9]{1,3}".[0-9]{1,3}".[0-9]{1,3}".)|((["w-]+".)+))([a-zA-Z]{2,4}|[0-9]{1,3})("]?)$";
  7    private const string REG_MOBILE = @"(^0{0,1}13[0-9]{9}$)";
  8    private const string REG_IDCARD = @"^([0-9]{14}|[0-9]{17})(x|[0-9]){1}$";
  9    private const string REG_TIME = @"^((([0-1]?[0-9])|(2[0-3]))([":])([0-5][0-9]))$";
 10    private const string REG_TIME1 = @"^((([0-1][0-9])|(2[0-3])):[0-5]"d:[0-5]"d)$";
 11    private const string REG_DATA1 = @"^(("d{2}(([02468][048])|([13579][26]))["-"/"s]((((0?[13578])|(1[02]))["-"/"s]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))["-"/"s]((0?[1-9])|([1-2][0-9])|(30)))|(0?2["-"/"s]((0?[1-9])|([1-2][0-9])))))|("d{2}(([02468][1235679])|([13579][01345789]))["-"/"s]((((0?[13578])|(1[02]))["-"/"s]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))["-"/"s]((0?[1-9])|([1-2][0-9])|(30)))|(0?2["-"/"s]((0?[1-9])|(1[0-9])|(2[0-8]))))))$";
 12    private const string REG_SHUZI = @"^"d+$";
 13
 14ContractedSubBlock.gifExpandedSubBlockStart.gif    #region 半角验证
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    /// <summary>
 16    /// 半角验证
 17    /// </summary>
 18    /// <param name="str">验证的字符串</param>
 19    /// <returns></returns>

 20    public static bool IsDBC(string str)
 21ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 22        UTF8Encoding encoding = new UTF8Encoding();
 23        int byteCount = encoding.GetByteCount(str);
 24        int strLen = str.Length;
 25
 26        if (strLen == byteCount)
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 28            return true;
 29        }

 30
 31        return false;
 32    }

 33    #endregion

 34
 35ContractedSubBlock.gifExpandedSubBlockStart.gif    #region 全角验证
 36ExpandedSubBlockStart.gifContractedSubBlock.gif    /// <summary>
 37    /// 全角验证
 38    /// </summary>
 39    /// <param name="str">验证的字符串</param>
 40    /// <returns></returns>

 41    public static bool IsSBC(string str)
 42ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 43        UTF8Encoding encoding = new UTF8Encoding();
 44        int byteCount = encoding.GetByteCount(str);
 45        int strLen = str.Length;
 46
 47        if (byteCount == strLen * 3)
 48ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 49            return true;
 50        }

 51
 52        return false;
 53    }

 54    #endregion

 55
 56
 57ContractedSubBlock.gifExpandedSubBlockStart.gif    #region 时间1字符串有效性验证
 58ExpandedSubBlockStart.gifContractedSubBlock.gif    /// <summary>
 59    /// 时间1字符串有效性验证
 60    /// </summary>hh:mm:ss
 61    /// <param name="date">日期字符串</param>
 62    /// <returns>有效:true,否则:false</returns>

 63    public static bool IsValidTime1(string time1)
 64ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 65        return Regex.IsMatch(time1, Common.REG_TIME1);
 66    }

 67    #endregion

 68
 69ContractedSubBlock.gifExpandedSubBlockStart.gif    #region 日期1字符串有效性验证
 70ExpandedSubBlockStart.gifContractedSubBlock.gif    /// <summary>
 71    /// 日期字符串有效性验证yyyy-mm-dd
 72    /// </summary>
 73    /// <param name="date">日期字符串</param>
 74    /// <returns>有效:true,否则:false</returns>

 75    public static bool IsValidDate1(string date1)
 76ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 77        return Regex.IsMatch(date1, Common.REG_DATA1);
 78    }

 79    #endregion

 80
 81
 82ContractedSubBlock.gifExpandedSubBlockStart.gif    #region 日期字符串有效性验证
 83ExpandedSubBlockStart.gifContractedSubBlock.gif    /// <summary>
 84    /// 日期字符串有效性验证
 85    /// </summary>
 86    /// <param name="date">日期字符串</param>
 87    /// <returns>有效:true,否则:false</returns>

 88    public static bool IsValidDate(string date)
 89ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 90        return Regex.IsMatch(date, Common.REG_DATE);
 91    }

 92    #endregion

 93
 94ContractedSubBlock.gifExpandedSubBlockStart.gif    #region Email有效性验证
 95ExpandedSubBlockStart.gifContractedSubBlock.gif    /// <summary>
 96    /// Email有效性验证
 97    /// </summary>
 98    /// <param name="email">Email字符串</param>
 99    /// <returns>有效:true,否则:false</returns>

100    public static bool IsValidEmail(string email)
101ExpandedSubBlockStart.gifContractedSubBlock.gif    {
102        return Regex.IsMatch(email, Common.REG_EMAIL);
103    }

104    #endregion

105
106ContractedSubBlock.gifExpandedSubBlockStart.gif    #region 电话号码有效性验证
107ExpandedSubBlockStart.gifContractedSubBlock.gif    /// <summary>
108    /// 电话号码有效性验证
109    /// </summary>
110    /// <param name="phone">电话号码字符串</param>
111    /// <returns>有效:true,否则:false</returns>

112    public static bool IsVaildPhone(string phone)
113ExpandedSubBlockStart.gifContractedSubBlock.gif    {
114        return Regex.IsMatch(phone, Common.REG_PHONE);
115    }

116    #endregion

117
118ContractedSubBlock.gifExpandedSubBlockStart.gif    #region 手机号码有效性验证
119ExpandedSubBlockStart.gifContractedSubBlock.gif    /// <summary>
120    /// 手机号码有效性验证
121    /// </summary>
122    /// <param name="mobile">手机号码字符串</param>
123    /// <returns>有效:true,否则:false</returns>

124    public static bool IsValidMobile(string mobile)
125ExpandedSubBlockStart.gifContractedSubBlock.gif    {
126        return Regex.IsMatch(mobile, REG_MOBILE);
127    }

128    #endregion

129
130ContractedSubBlock.gifExpandedSubBlockStart.gif    #region 身份证号有效性验证
131ExpandedSubBlockStart.gifContractedSubBlock.gif    /// <summary>
132    /// 身份证号有效性验证
133    /// </summary>
134    /// <param name="idCard">身份证号字符串</param>
135    /// <returns>有效:true,否则:false</returns>

136    public static bool IsValidIdCard(string idCard)
137ExpandedSubBlockStart.gifContractedSubBlock.gif    {
138        return Regex.IsMatch(idCard, Common.REG_IDCARD);
139    }

140    #endregion

141
142ContractedSubBlock.gifExpandedSubBlockStart.gif    #region 日期字符串转换成日期对象
143ExpandedSubBlockStart.gifContractedSubBlock.gif    /// <summary>
144    /// 日期字符串转换成日期对象
145    /// </summary>
146    /// <param name="date">日期字符串</param>
147    /// <returns>日期对象</returns>

148    public static DateTime CastDateTime(string date)
149ExpandedSubBlockStart.gifContractedSubBlock.gif    {
150        StringBuilder builder = new StringBuilder();
151        builder.Append(date.Substring(04));
152        builder.Append("-");
153        builder.Append(date.Substring(42));
154        builder.Append("-");
155        builder.Append(date.Substring(62));
156
157        return Convert.ToDateTime(builder.ToString());
158    }

159    #endregion

160
161ContractedSubBlock.gifExpandedSubBlockStart.gif    #region 日期对象转化成日期字符串
162ExpandedSubBlockStart.gifContractedSubBlock.gif    /// <summary>
163    /// 日期对象转化成日期字符串
164    /// </summary>
165    /// <param name="date">日期对象</param>
166    /// <returns>日期字符串</returns>

167    public static string CastDateTime(DateTime date)
168ExpandedSubBlockStart.gifContractedSubBlock.gif    {
169        string strDate = date.ToString("yyyy-MM-dd");
170        strDate = strDate.Replace("-""");
171        return strDate;
172    }

173    #endregion

174
175ContractedSubBlock.gifExpandedSubBlockStart.gif    #region 时间格式验证
176ExpandedSubBlockStart.gifContractedSubBlock.gif    /// <summary>
177    /// 时间格式验证
178    /// </summary>
179    /// <param name="time">时间字符串</param>
180    /// <returns>正确:true,错误:false</returns>

181    public static bool IsValidTime(string time)
182ExpandedSubBlockStart.gifContractedSubBlock.gif    {
183        return Regex.IsMatch(time, REG_TIME);
184    }

185    #endregion

186
187ContractedSubBlock.gifExpandedSubBlockStart.gif    #region 数字验证
188ExpandedSubBlockStart.gifContractedSubBlock.gif    /// <summary>
189    /// 数字验证
190    /// </summary>
191    /// <returns>正确:true,错误:false</returns>

192    public static bool IsValidSHUZI(string shuzi)
193ExpandedSubBlockStart.gifContractedSubBlock.gif    {
194        return Regex.IsMatch(shuzi, REG_SHUZI);
195    }

196    #endregion

197
198ContractedSubBlock.gifExpandedSubBlockStart.gif    #region 年龄校验
199    public static bool IsAges(string str)
200ExpandedSubBlockStart.gifContractedSubBlock.gif    {
201        System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^"d+$");
202        if (reg1.IsMatch(str))
203ExpandedSubBlockStart.gifContractedSubBlock.gif        {
204            if (Convert.ToInt32(str.Trim()) > 18 && Convert.ToInt32(str.Trim()) <= 100)
205ExpandedSubBlockStart.gifContractedSubBlock.gif            {
206                return true;
207            }

208            else
209ExpandedSubBlockStart.gifContractedSubBlock.gif            {
210                return false;
211            }

212        }

213        else
214ExpandedSubBlockStart.gifContractedSubBlock.gif        {
215            return false;
216        }

217    }

218    #endregion

219}

220

转载于:https://www.cnblogs.com/jizhitao/archive/2009/10/13/1582494.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值