当前位置: 移动技术网 > IT编程>开发语言>c# > C#常用正则验证函数示例

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#常用正则验证函数。分享给大家供大家参考,具体如下:

1、ip地址验证

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

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正则表达式在线测试工具:

正则表达式在线生成工具:

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网