当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 几个C#常用正则表达式的总结

ASP.NET
.Net技术开发中两个“属性”引起的歧异
技术文档:解读.Net虚拟框架的实现原理
.Net课堂:总结必须学习的10项.NET技术
实现MSMQ消息加密的安全实践
C#中对DatagridView的部分常用操作
.Net基础:了解ASP.NET中的IFRAME框架挂马
ASP.NET中显示Linq To SQL输出的SQL语句
链表的顺序表示和实现(C++模板类实现)
如何在ASP.NET项目里面正确使用Linq to Sql
ASP.NET两个截取字符串的实用方法技巧
一个简单程序的反编译
ASP.NET MVC中你必须知道的13个扩展点
Entity Framework的默认值BUG解决方法
C#中通过Assembly类访问程序集信息
Java与.NET间进行Web Service交互的选择
C#中用鼠标移动页面功能的实现
程序员的信仰
ASP.NET多附件上传和附件编辑的实现
菜鸟课堂:在Visual C# .NET中跟踪和调试
IronPython和C#执行速度对比

ASP.NET 中的 几个C#常用正则表达式的总结


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 39 ::
收藏到网摘: n/a

using System;
using System.Text.RegularExpressions;
namespace CommonTools
{
/**//// <summary>
/// RegexLib 的摘要说明。
/// </summary>
public class RegexLib
{
//验证Email地址
public static bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
//dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。
public static string MDYToDMY(String input)
{
return Regex.Replace(input,"\\b(?\\d{1,2})/(?\\d{1,2})/(?\\d{2,4})\\b","${day}-${month}-${year}");
}
//验证是否为小数
public static bool IsValidDecimal(string strIn)
{
return Regex.IsMatch(strIn,@"[0].\d{1,2}|[1]");
}
//验证是否为电话号码
public static bool IsValidTel(string strIn)
{
return Regex.IsMatch(strIn,@"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?");
}
//验证年月日
public static bool IsValidDate(string strIn)
{
return Regex.IsMatch(strIn,@"^2\d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]\d|3[0-1])(?:0?[1-9]|1\d|2[0-3]):(?:0?[1-9]|[1-5]\d):(?:0?[1-9]|[1-5]\d)$");
}
//验证后缀名
public static bool IsValidPostfix(string strIn)
{
return Regex.IsMatch(strIn,@"\.(?i:gif|jpg)$");
}
//验证字符是否再4至12之间
public static bool IsValidByte(string strIn)
{
return Regex.IsMatch(strIn,@"^[a-z]{4,12}$");
}
//验证IP
public static bool IsValidIp(string strIn)
{
return Regex.IsMatch(strIn,@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
}
}
}