加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:11:06 所属栏目:百科 来源:网络整理
导读:本篇章节讲解C#常用正则验证函数。供大家参考研究具体如下: 1、Ip地址验证 /// summary/// Ip地址验证/// /summarypublic static bool CheckIp(string ip){ bool result = false; Regex ipReg = new Regex(@"^(d{1,2}|1dd|2[0-4]d|25[0-5]).

本篇章节讲解C#常用正则验证函数。分享给大家供大家参考,具体如下:

1、Ip地址验证

/// <summary>
/// Ip地址验证
/// </summary>
public static bool CheckIp(string ip)
{
  bool result = false;
  Regex ipReg = new Regex(@"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$");
  if (ipReg.IsMatch(ip))
  {
    result = true;
  }
  return result;
}

2、价格验证

/// <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;
}

3、正整数验证

/// <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;
}

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.aspzz.cn/regex/javascript

正则表达式在线生成工具:
http://tools.aspzz.cn/regex/create_reg

更多关于C#相关内容感兴趣的读者可查看本站专题:《C#正则表达式用法总结》、《C#编码操作技巧总结》、《C#中XML文件操作技巧汇总》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》

希望本文所述对大家C#程序设计有所帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读