C#常用正则验证函数示例
本文实例讲述了C#常用正则验证函数。分享给大家供大家参考,具体如下:
1、Ip地址验证
///<summary>
///Ip地址验证
///</summary>
publicstaticboolCheckIp(stringip)
{
boolresult=false;
RegexipReg=newRegex(@"^(\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])$");
if(ipReg.IsMatch(ip))
{
result=true;
}
returnresult;
}
2、价格验证
///<summary>
///价格验证
///</summary>
///<paramname="priceStr"></param>
///<returns></returns>
publicboolCheckPrice(stringpriceStr)
{
boolresult=false;
Regexregex=newRegex(@"^\d+(\.\d{1,2})?$",RegexOptions.IgnoreCase);
Matchmatch=regex.Match(priceStr);
if(match.Success)
{
result=true;
}
returnresult;
}
3、正整数验证
///<summary>
///正整数验证
///</summary>
publicstaticboolCheckPositiveInteger(stringnumStr)
{
boolresult=false;
Regexregex=newRegex(@"^[1-9]\d*$",RegexOptions.IgnoreCase);
Matchmatch=regex.Match(numStr);
if(match.Success)
{
result=true;
}
returnresult;
}
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript
正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg
更多关于C#相关内容感兴趣的读者可查看本站专题:《C#正则表达式用法总结》、《C#编码操作技巧总结》、《C#中XML文件操作技巧汇总》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》
希望本文所述对大家C#程序设计有所帮助。