源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#常用正则验证函数示例

  • 时间:2022-12-22 07:08 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#常用正则验证函数示例
本文实例讲述了C#常用正则验证函数。分享给大家供大家参考,具体如下: [b]1、Ip地址验证[/b]
/// <summary>
/// Ip地址验证
/// </summary>
public static bool CheckIp(string ip)
{
  bool result = false;
  Regex ipReg = new Regex(@"^(\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;
  }
  return result;
}

[b]2、价格验证[/b]
/// <summary>
/// 价格验证
/// </summary>
/// <param name="priceStr"></param>
/// <returns></returns>
public bool CheckPrice(string priceStr)
{
  bool result = false;
  Regex regex = new Regex(@"^\d+(\.\d{1,2})?$", RegexOptions.IgnoreCase);
  Match match = regex.Match(priceStr);
  if (match.Success)
  {
    result = true;
  }
  return result;
}

[b]3、正整数验证[/b]
/// <summary>
/// 正整数验证
/// </summary>
public static bool CheckPositiveInteger(string numStr)
{
  bool result = false;
  Regex regex = new Regex(@"^[1-9]\d*$", RegexOptions.IgnoreCase);
  Match match = regex.Match(numStr);
  if (match.Success)
  {
    result = true;
  }
  return result;
}

[b]PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:[/b] [b]JavaScript正则表达式在线测试工具: [/b][url=http://tools.jb51.net/regex/javascript]http://tools.jb51.net/regex/javascript[/url] [b]正则表达式在线生成工具: [/b][url=http://tools.jb51.net/regex/create_reg]http://tools.jb51.net/regex/create_reg[/url] 更多关于C#相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/344.htm]C#正则表达式用法总结[/url]》、《[url=http://www.1sucai.cn/Special/792.htm]C#编码操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/266.htm]C#中XML文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/116.htm]C#数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/478.htm]C#面向对象程序设计入门教程[/url]》及《[url=http://www.1sucai.cn/Special/227.htm]C#程序设计之线程使用技巧总结[/url]》 希望本文所述对大家C#程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部