代码改变世界
2005-06-08 13:32
张剑
阅读(709)
评论()
编辑
收藏
举报
using System;
using System.Text.RegularExpressions;
![](/Images/OutliningIndicators/None.gif)
namespace bobomousecom.crm
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Regexlib 的摘要说明。
/// </summary>
public class Regexlib
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Regexlib()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//搜索输入字符串并返回所有 href=“
”值
string DumpHrefs(String inputString)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Regex r;
Match m;
![](/Images/OutliningIndicators/InBlock.gif)
r = new Regex("href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))",
RegexOptions.IgnoreCase|RegexOptions.Compiled);
for (m = r.Match(inputString); m.Success; m = m.NextMatch())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return("Found href " + m.Groups[1]);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//验证Email地址
bool IsValidEmail(string strIn)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 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})(\]?)$");
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。
string MDYToDMY(String input)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Regex.Replace(input,"\\b(?\\d{1,2})/(?\\d{1,2})/(?\\d{2,4})\\b","${day}-${month}-${year}");
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//验证是否为小数
bool IsValidDecimal(string strIn)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Regex.IsMatch(strIn,@"[0].\d{1,2}|[1]");
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//验证是否为电话号码
bool IsValidTel(string strIn)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Regex.IsMatch(strIn,@"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?");
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//验证年月日
bool IsValidDate(string strIn)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)$");
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//验证后缀名
bool IsValidPostfix(string strIn)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Regex.IsMatch(strIn,@"\.(?i:gif|jpg)$");
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//验证字符是否在4至12之间
bool IsValidByte(string strIn)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Regex.IsMatch(strIn,@"^[a-z]{4,12}$");
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//验证IP
bool IsValidIp(string strIn)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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])$");
}
}
}
![](/Images/OutliningIndicators/None.gif)