常用正则表达式
来源:互联网 发布:淘宝信用贷款上征信吗 编辑:程序博客网 时间:2024/06/03 22:40
/// <summary>
/// 检验日期格式是否正确
/// </summary>
#region public string IsDateFormat(string strDate)
public string IsDateFormat(string strDate)
{
strDate = strDate.Trim();
Regex r1 = new Regex(@"^(?<year>[1-9][0-9]{0,3})/(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})$");
Regex r2 = new Regex(@"^(?<year>[1-9][0-9]{0,3})-(?<month>[0-9]{1,2})-(?<day>[0-9]{1,2})$");
Regex r3 = new Regex(@"^(?<year>[1-9][0-9]{0,3})年(?<month>[0-9]{1,2})月(?<day>[0-9]{1,2})日$");
Regex r4 = new Regex(@"^(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})/(?<year>[1-9][0-9]{0,3})$");
// 取得日期的年,月,日
string year, month, date;
if(Regex.IsMatch(strDate,@"^(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})/(?<year>[1-9][0-9]{3})$"))
{
year = r4.Match(strDate).Result("${year}");
month = r4.Match(strDate).Result("${month}");
date = r4.Match(strDate).Result("${day}");
}
else if (Regex.IsMatch(strDate,@"^(?<year>[1-9][0-9]{0,3})/(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})$"))
{
year = r1.Match(strDate).Result("${year}");
month = r1.Match(strDate).Result("${month}");
date = r1.Match(strDate).Result("${day}");
}
else if(Regex.IsMatch(strDate,@"^(?<year>[1-9][0-9]{0,3})-(?<month>[0-9]{1,2})-(?<day>[0-9]{1,2})$"))
{
year = r2.Match(strDate).Result("${year}");
month = r2.Match(strDate).Result("${month}");
date = r2.Match(strDate).Result("${day}");
}
else if(Regex.IsMatch(strDate,@"^(?<year>[1-9][0-9]{0,3})年(?<month>[0-9]{1,2})月(?<day>[0-9]{1,2})日$"))
{
year = r3.Match(strDate).Result("${year}");
month = r3.Match(strDate).Result("${month}");
date = r3.Match(strDate).Result("${day}");
}
else
{
return "error";
}
// 最后检查日期的正确性
try
{
System.DateTime dt = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(date));
return dt.ToString("yyyy-MM-dd");
}
catch
{
return "error";
}
}
#endregion
------------------------------------------------------------------------------------------
/// <summary>
/// 检验Email字符串格式是否正确
/// </summary>
#region public bool IsEmailFormat(string strEmail)
public bool IsEmailFormat(string strEmail)
{
return Regex.IsMatch(strEmail, @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
}
#endregion
------------------------------------------------------------------------------------------
/// <summary>
/// 转换十五位身份证为十八位的函数。
/// </summary>
#region public string ConvertIDCard15to18(string strTemp)
public string ConvertIDCard15to18(string strTemp)
{
int[] arrInt = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
string arrCh="10X98765432";
int nTemp = 0;
if(strTemp.Length==15)
{
strTemp = strTemp.Substring(0,6) + "19" + strTemp.Substring(6,strTemp.Length-6);
for(int i = 0; i < strTemp.Length; i++)
{
nTemp += int.Parse(strTemp.Substring(i, 1).ToString()) * arrInt[i];
}
strTemp += arrCh[nTemp % 11];
}
char dd=arrCh[nTemp % 11];
return strTemp;
}
#endregion
------------------------------------------------------------------------------------------
下在为判断ASCII码的函数组,仅支持中英文
/// <summary>
/// 是否为双字节字符。
/// </summary>
public static bool IsTwoBytesChar(char chr)
{
string str =chr.ToString();
// 使用中文支持编码
Encoding ecode = Encoding.GetEncoding("GB18030");
if (ecode.GetByteCount(str) == 2)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 得到字符的ASCII码
/// </summary>
public static int ASCII(char chr)
{
Encoding ecode = Encoding.GetEncoding("GB18030");
Byte[] codeBytes = ecode.GetBytes(chr.ToString());
if ( IsTwoBytesChar(chr) )
{
// 双字节码为高位乘256,再加低位
// 该为无符号码,再减65536
return (int)codeBytes[0] * 256 + (int)codeBytes[1] - 65536;
}
else
{
return (int)codeBytes[0];
}
}
/// 检验日期格式是否正确
/// </summary>
#region public string IsDateFormat(string strDate)
public string IsDateFormat(string strDate)
{
strDate = strDate.Trim();
Regex r1 = new Regex(@"^(?<year>[1-9][0-9]{0,3})/(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})$");
Regex r2 = new Regex(@"^(?<year>[1-9][0-9]{0,3})-(?<month>[0-9]{1,2})-(?<day>[0-9]{1,2})$");
Regex r3 = new Regex(@"^(?<year>[1-9][0-9]{0,3})年(?<month>[0-9]{1,2})月(?<day>[0-9]{1,2})日$");
Regex r4 = new Regex(@"^(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})/(?<year>[1-9][0-9]{0,3})$");
// 取得日期的年,月,日
string year, month, date;
if(Regex.IsMatch(strDate,@"^(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})/(?<year>[1-9][0-9]{3})$"))
{
year = r4.Match(strDate).Result("${year}");
month = r4.Match(strDate).Result("${month}");
date = r4.Match(strDate).Result("${day}");
}
else if (Regex.IsMatch(strDate,@"^(?<year>[1-9][0-9]{0,3})/(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})$"))
{
year = r1.Match(strDate).Result("${year}");
month = r1.Match(strDate).Result("${month}");
date = r1.Match(strDate).Result("${day}");
}
else if(Regex.IsMatch(strDate,@"^(?<year>[1-9][0-9]{0,3})-(?<month>[0-9]{1,2})-(?<day>[0-9]{1,2})$"))
{
year = r2.Match(strDate).Result("${year}");
month = r2.Match(strDate).Result("${month}");
date = r2.Match(strDate).Result("${day}");
}
else if(Regex.IsMatch(strDate,@"^(?<year>[1-9][0-9]{0,3})年(?<month>[0-9]{1,2})月(?<day>[0-9]{1,2})日$"))
{
year = r3.Match(strDate).Result("${year}");
month = r3.Match(strDate).Result("${month}");
date = r3.Match(strDate).Result("${day}");
}
else
{
return "error";
}
// 最后检查日期的正确性
try
{
System.DateTime dt = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(date));
return dt.ToString("yyyy-MM-dd");
}
catch
{
return "error";
}
}
#endregion
------------------------------------------------------------------------------------------
/// <summary>
/// 检验Email字符串格式是否正确
/// </summary>
#region public bool IsEmailFormat(string strEmail)
public bool IsEmailFormat(string strEmail)
{
return Regex.IsMatch(strEmail, @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
}
#endregion
------------------------------------------------------------------------------------------
/// <summary>
/// 转换十五位身份证为十八位的函数。
/// </summary>
#region public string ConvertIDCard15to18(string strTemp)
public string ConvertIDCard15to18(string strTemp)
{
int[] arrInt = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
string arrCh="10X98765432";
int nTemp = 0;
if(strTemp.Length==15)
{
strTemp = strTemp.Substring(0,6) + "19" + strTemp.Substring(6,strTemp.Length-6);
for(int i = 0; i < strTemp.Length; i++)
{
nTemp += int.Parse(strTemp.Substring(i, 1).ToString()) * arrInt[i];
}
strTemp += arrCh[nTemp % 11];
}
char dd=arrCh[nTemp % 11];
return strTemp;
}
#endregion
------------------------------------------------------------------------------------------
下在为判断ASCII码的函数组,仅支持中英文
/// <summary>
/// 是否为双字节字符。
/// </summary>
public static bool IsTwoBytesChar(char chr)
{
string str =chr.ToString();
// 使用中文支持编码
Encoding ecode = Encoding.GetEncoding("GB18030");
if (ecode.GetByteCount(str) == 2)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 得到字符的ASCII码
/// </summary>
public static int ASCII(char chr)
{
Encoding ecode = Encoding.GetEncoding("GB18030");
Byte[] codeBytes = ecode.GetBytes(chr.ToString());
if ( IsTwoBytesChar(chr) )
{
// 双字节码为高位乘256,再加低位
// 该为无符号码,再减65536
return (int)codeBytes[0] * 256 + (int)codeBytes[1] - 65536;
}
else
{
return (int)codeBytes[0];
}
}
- 【正则】常用正则表达式
- 正则表达式-常用正则表达式
- 正则表达式 常用正则表达式
- 正则表达式---常用的正则
- 正则表达式---常用的正则
- 【正则】常用正则表达式收集
- 正则大全 常用正则表达式
- 正则表达式 常用的正则
- .net正则表达式(常用表达式)
- 正则表达式中常用表达式
- 常用的正则表达式
- 常用正则表达式
- 常用的正则表达式
- 常用正则表达式参考
- 常用正则表达式收藏
- 常用C#正则表达式
- 常用正则表达式
- 常用正则表达式实例
- 字符串操作常用函数
- 计算两个日期之间的间隔天数、周末天数、以及为当年的哪一周
- 关于客户端加载事件-updatapanel
- Singleton - Design Patterns学习笔记(1)
- linux修改主机名的方法
- 常用正则表达式
- Subversion之路--实现精细的目录访问权限控制
- BIOS详解
- 盗贼开锁技能问题(详细)
- windowsXP 装sql server2000 文件挂起
- 取得月份的天数
- nhibernate源码分析之八: 解析HQL
- SoftKey.Solutions.SENTINEL.Emulator.2007-EDGE
- Arteam: Activemark 6.2 Dumper by condzero