/***********************************************************
 * 文件: MetarnetRegex.cs
 * 日期: 2006-07-25
 **********************************************************/
using System;
using System.Text.RegularExpressions;
namespace MetarCommonSupport
{
 /// <summary>
 /// 通过Framwork类库中的Regex类实现了一些特殊功能数据检查
 /// </summary>
 public class MetarnetRegex
 {
  
  private static MetarnetRegex instance = null;
  public static MetarnetRegex GetInstance()
  {
   if(MetarnetRegex.instance == null)
   {
    MetarnetRegex.instance = new MetarnetRegex();
   }
   return MetarnetRegex.instance;
  }
  private MetarnetRegex()
  {
  }
  /// <summary>
  /// 判断输入的字符串只包含汉字
  /// </summary>
  /// <param name="input"></param>
  /// <returns></returns>
  public static bool IsChineseCh(string input)
  {
   Regex regex = new Regex("^[\u4e00-\u9fa5]+$");
   return regex.IsMatch(input);
  }

/// <summary>
  /// 匹配3位或4位区号的电话号码,其中区号可以用小括号括起来,
  /// 也可以不用,区号与本地号间可以用连字号或空格间隔,
  /// 也可以没有间隔
  /// \(0\d{2}\)[- ]?\d{8}|0\d{2}[- ]?\d{8}|\(0\d{3}\)[- ]?\d{7}|0\d{3}[- ]?\d{7}
  /// </summary>
  /// <param name="input"></param>
  /// <returns></returns>
  public static bool IsPhone(string input)
  {
   string pattern = "^\\(0\\d{2}\\)[- ]?\\d{8}$|^0\\d{2}[- ]?\\d{8}$|^\\(0\\d{3}\\)[- ]?\\d{7}$|^0\\d{3}[- ]?\\d{7}$";
   Regex regex = new Regex(pattern);
   return regex.IsMatch(input);
  }
  /// <summary>
  /// 判断输入的字符串是否是一个合法的手机号
  /// </summary>
  /// <param name="input"></param>
  /// <returns></returns>
  public static bool IsMobilePhone(string input)
  {
   Regex regex = new Regex("^13\\d{9}$");
   return regex.IsMatch(input);
   
  }

/// <summary>
  /// 判断输入的字符串只包含数字
  /// 可以匹配整数和浮点数
  /// ^-?\d+$|^(-?\d+)(\.\d+)?$
  /// </summary>
  /// <param name="input"></param>
  /// <returns></returns>
  public static bool IsNumber(string input)
  {
   string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
   Regex regex = new Regex(pattern);
   return regex.IsMatch(input);
  }
  /// <summary>
  /// 匹配非负整数
  ///
  /// </summary>
  /// <param name="input"></param>
  /// <returns></returns>
  public static bool IsNotNagtive(string input)
  {
   Regex regex = new Regex(@"^\d+$");
   return regex.IsMatch(input);
  }
  /// <summary>
  /// 匹配正整数
  /// </summary>
  /// <param name="input"></param>
  /// <returns></returns>
  public static bool IsUint(string input)
  {
   Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
   return regex.IsMatch(input);
  }
  /// <summary>
  /// 判断输入的字符串字包含英文字母
  /// </summary>
  /// <param name="input"></param>
  /// <returns></returns>
  public static bool IsEnglisCh(string input)
  {
   Regex regex = new Regex("^[A-Za-z]+$");
   return regex.IsMatch(input);
  }

/// <summary>
  /// 判断输入的字符串是否是一个合法的Email地址
  /// </summary>
  /// <param name="input"></param>
  /// <returns></returns>
  public static bool IsEmail(string input)
  {
   string pattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
   Regex regex = new Regex(pattern);
   return regex.IsMatch(input);
  }

/// <summary>
  /// 判断输入的字符串是否只包含数字和英文字母
  /// </summary>
  /// <param name="input"></param>
  /// <returns></returns>
  public static bool IsNumAndEnCh(string input)
  {
   string pattern = @"^[A-Za-z0-9]+$";
   Regex regex = new Regex(pattern);
   return regex.IsMatch(input);
  }

/// <summary>
  /// 判断输入的字符串是否是一个超链接
  /// </summary>
  /// <param name="input"></param>
  /// <returns></returns>
  public static bool IsURL(string input)
  {
   //string pattern = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
   string pattern = @"^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$";
   Regex regex = new Regex(pattern);
   return regex.IsMatch(input);
  }

/// <summary>
  /// 判断输入的字符串是否是表示一个IP地址
  /// </summary>
  /// <param name="input">被比较的字符串</param>
  /// <returns>是IP地址则为True</returns>
  public static bool IsIPv4(string input)
  {
   
   string[] IPs = input.Split('.');
   Regex regex = new Regex(@"^\d+$");
   for(int i = 0; i<IPs.Length; i++)
   {
    if(!regex.IsMatch(IPs[i]))
    {
     return false;
    }
    if(Convert.ToUInt16(IPs[i]) > 255)
    {
     return false;
    }
   }
   return true;
  }

/// <summary>
  /// 计算字符串的字符长度,一个汉字字符将被计算为两个字符
  /// </summary>
  /// <param name="input">需要计算的字符串</param>
  /// <returns>返回字符串的长度</returns>
  public static int GetCount(string input)
  {
   return Regex.Replace(input,@"[\u4e00-\u9fa5/g]","aa").Length;
  }

/// <summary>
  /// 调用Regex中IsMatch函数实现一般的正则表达式匹配
  /// </summary>
  /// <param name="pattern">要匹配的正则表达式模式。</param>
  /// <param name="input">要搜索匹配项的字符串</param>
  /// <returns>如果正则表达式找到匹配项,则为 true;否则,为 false。</returns>
  public static bool IsMatch(string pattern, string input)
  {
   Regex regex = new Regex(pattern);
   return regex.IsMatch(input);
  }
  
  /// <summary>
  /// 从输入字符串中的第一个字符开始,用替换字符串替换指定的正则表达式模式的所有匹配项。
  /// </summary>
  /// <param name="pattern">模式字符串</param>
  /// <param name="input">输入字符串</param>
  /// <param name="replacement">用于替换的字符串</param>
  /// <returns>返回被替换后的结果</returns>
  public static string Replace(string pattern, string input, string replacement)
  {
   Regex regex = new Regex(pattern);
   return regex.Replace(input,replacement);
  }

/// <summary>
  /// 在由正则表达式模式定义的位置拆分输入字符串。
  /// </summary>
  /// <param name="pattern">模式字符串</param>
  /// <param name="input">输入字符串</param>
  /// <returns></returns>
  public static string[] Split(string pattern, string input)
  {
   Regex regex = new Regex(pattern);
   return regex.Split(input);
  }
  /// <summary>
  /// 判断输入的字符串是否是合法的IPV6 地址
  /// </summary>
  /// <param name="input"></param>
  /// <returns></returns>
  public static bool IsIPV6(string input)
  {
   string pattern = "";
   string temp = input;
   string[] strs = temp.Split(':');
   if(strs.Length > 8)
   {
    return false;
   }
   int count = MetarnetRegex.GetStringCount(input,"::");
   if(count>1)
   {
    return false;
   }
   else if(count == 0)
   {
    pattern = @"^([\da-f]{1,4}:){7}[\da-f]{1,4}$";

Regex regex = new Regex(pattern);
    return regex.IsMatch(input);
   }
   else
   {
    pattern = @"^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$";
    Regex regex1 = new Regex(pattern);
    return regex1.IsMatch(input);
   }

}
  /* *******************************************************************
   * 1、通过“:”来分割字符串看得到的字符串数组长度是否小于等于8
   * 2、判断输入的IPV6字符串中是否有“::”。
   * 3、如果没有“::”采用 ^([\da-f]{1,4}:){7}[\da-f]{1,4}$ 来判断
   * 4、如果有“::” ,判断"::"是否止出现一次
   * 5、如果出现一次以上 返回false
   * 6、^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$
   * ******************************************************************/
  /// <summary>
  /// 判断字符串compare 在 input字符串中出现的次数
  /// </summary>
  /// <param name="input">源字符串</param>
  /// <param name="compare">用于比较的字符串</param>
  /// <returns>字符串compare 在 input字符串中出现的次数</returns>
  private static int GetStringCount(string input, string compare)
  {
   int index = input.IndexOf(compare);
   if(index != -1)
   {
    return 1 + GetStringCount(input.Substring(index + compare.Length),compare);
   }
   else
   {
    return 0;
   }

}
 }
}

用C# Regex类实现的一些常规输入判断相关推荐

  1. C# Regex类详解

    using System; using System.Text.RegularExpressions; namespace MetarCommonSupport { /// <summary&g ...

  2. C#正则表达式编程(二):Regex类用法

    上一篇讲述了在C#中有关正则表达式的类之间的关系,以及它们的方法,这一篇主要是将Regex这个类的用法的,关于Match及MatchCollection类会在下一篇讲到. 对于正则表达式的应用,基本上 ...

  3. C#正则表达式Regex类的用法

    一.C#正则表达式符号模式 字 符 描 述 \ 转义字符,将一个具有特殊功能的字符转义为一个普通字符,或反过来 ^ 匹配输入字符串的开始位置 $ 匹配输入字符串的结束位置 * 匹配前面的零次或多次的子 ...

  4. c#中regex的命名空间_C# Regex类用法

    使用Regex类需要引用命名空间:using System.Text.RegularExpressions; 利用Regex类实现全部匹配输出 string str = "test43232 ...

  5. C#中使用Regex类来实现正则表达式

    前言 在上一篇的文章中,我们介绍了正则表达式的基本语法规则以及含义.那么在编写脚本时我们要如何使用它们呢?在不同的语言中,基本都提供了相对应的类库帮助我们实现,本文主要介绍正则在C#中的使用方法. R ...

  6. c#中regex的命名空间_C#_详解C#正则表达式Regex常用匹配,使用Regex类需要引用命名空间 - phpStudy...

    详解C#正则表达式Regex常用匹配 使用Regex类需要引用命名空间:using System.Text.RegularExpressions; 一.利用Regex类实现验证 示例1:注释的代码所起 ...

  7. C#正则表达式RegularExpression相关知识(Regex类使用详情)

    C#正则表达式相关知识 本文档为本人学习总结,义务提供,不作为商用,侵权请联系删除 本文档部分内容为转载,转载处在下会标明 1. 原样匹配 任意字符:原样匹配任意字符 例如:123,会匹配一串字符中是 ...

  8. C++类的使用(六)—— 判断继承

    相信通过之前的学习,你也已经对类有了深刻的了解,那么请利用你所学的知识,想一想下面这道题. /* 函数名: foo* 函数声明:* template <class B, class D>* ...

  9. 编写一个类的方法,其输入参数为一个整数,输出为该整数各个位上的最大数字

    1. 编写一个类的方法,其输入参数为一个整数,输出为该整数各个位上的最大数字. import java.util.*;public class Main {public static int s(in ...

最新文章

  1. 32位jdk_MyEclipse 10((32/64位)、(MAC)、(Linux))软件安装教程
  2. 管理和安装 chart - 每天5分钟玩转 Docker 容器技术(168)
  3. Android音频底层调试-基于tinyalsa
  4. vscode - 添加背景图片
  5. 浅谈C++类(5)--友元
  6. wcg总决赛_关于总决赛
  7. 阻止html页面加载,如何防止页面加载重复的JS?
  8. leetcode解题记录(二)
  9. 4G + 1G = 5G?
  10. oracle 设置不可重复,oracle – 不可重复读和幻读之间有什么区别?
  11. 同一操作系统中安装多个不同版本谷歌Chrome浏览器
  12. 随机森林在sklearn中的实现
  13. Excel如何实现多条件计数统计
  14. 有的小伙伴问我英语不好可以学编程吗?
  15. Linux中使用者身份的切换su和sudo的用法
  16. ui设计范畴,ui设计分为哪几类
  17. android 高德地图 删除多边形,高德地图多边形覆盖物添加、获取、删除
  18. ①编写一个程序,从键盘接收一个字符串,然后按照字符顺序从小到大进行排序,并删除重复的字符。②集合A、B的差集③对分行输入的若干字符串按字典序(由小到大)进行排序并输出。
  19. JVM的内存区域划分(jdk7和jdk8)
  20. FZU 1076 穿越沙漠(逆推建模)(数学)

热门文章

  1. mysql show status 过滤_给MySQL的show table status结果做过滤
  2. Win11怎么把右键刷新调出来?
  3. springBoot跨域注解@CrossOrigin
  4. vim设置tab宽度为4_vim编辑器VimScript插件开发系列一「定制Vim 3」
  5. 1470. 重新排列数组
  6. 在平常网上原来是这样购票的(模拟购票)
  7. php trait编译实现,为什么PHP Trait不能实现接口?
  8. 日常学习随笔-数组、单链表、双链表三种形式实现队列结构的基本操作(源码注释)...
  9. 201803-1-跳一跳
  10. html基础内容样式