Dot net 正则表达式的应用,也是dot net 开发中不可缺少的东东
public class RegularFun
{
public RegularFun()
{
}
/// <summary>
/// 判断一个数字是否整数
/// </summary>
/// <param name="strNumber">字符串</param>
/// <returns></returns>
public static bool IsInteger(string strNumber)
{
Regex objNotIntPattern=new Regex("[^0-9-]");
Regex objIntPattern=new Regex("^-[0-9]+$|^[0-9]+$");
return !objNotIntPattern.IsMatch(strNumber) &&
objIntPattern.IsMatch(strNumber);
}
/// <summary>
///判断是否只有字符和数字组成
/// </summary>
/// <param name="strToCheck"></param>
/// <returns></returns>
public static bool IsAlphaNumeric(String strToCheck)
{
Regex objAlphaNumericPattern=new Regex("[^a-zA-Z0-9]");
return !objAlphaNumericPattern.IsMatch(strToCheck);
}
/// <summary>
/// 校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static bool IsRegUserName(string userName)
{
Regex objAlphaNumericPattern=new Regex("^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$");
return !objAlphaNumericPattern.IsMatch(userName);
}
/// <summary>
/// 校验密码:只能输入6-20个字母、数字、下划线
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static bool IsPassword(string password)
{
Regex objAlphaNumericPattern=new Regex(@"^(\w){6,20}$");
return !objAlphaNumericPattern.IsMatch(password);
}
/// <summary>
/// 校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”
/// </summary>
/// <param name="tel"></param>
/// <returns></returns>
public static bool IsTelepone(string tel)
{
Regex objAlphaNumericPattern=new Regex(@"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
return !objAlphaNumericPattern.IsMatch(tel);
}
/// <summary>
/// 校验手机号码:必须以数字开头,除数字外,可含有“-”
/// </summary>
/// <param name="mobile"></param>
/// <returns></returns>
public static bool IsMobil(string mobile)
{
Regex objAlphaNumericPattern=new Regex(@"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
return !objAlphaNumericPattern.IsMatch(mobile);
}
/// <summary>
/// 校验IP地址
/// </summary>
/// <param name="ipaddr"></param>
/// <returns></returns>
public static bool IsIPAddress(string ipaddr)
{
Regex objAlphaNumericPattern=new Regex(@"^[0-9.]{1,20}$");
return !objAlphaNumericPattern.IsMatch(ipaddr);
}
}