`
v5browser
  • 浏览: 1134587 次
社区版块
存档分类
最新评论

C#判断各种字符串(如手机号)

 
阅读更多
 
 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#-字符串操作类

    、指定字符串分割字符串、指定字符串位置获取字符串、过滤SQL中非法字符、检查SQL语句中是否有非法关键字、随机字符串生成、唯一字符串生成、随机数字生成、唯一数字串生成、去除HTML标签、判断是否整型、判断是否...

    正则表达式判断类集合C#

    C#一个有关正则表达式判断类的集合,包含:判断输入的字符串是否是一个合法的手机号等。

    C#各种验证帮助类库项目ValidatorHelper.zip

    各种验证帮助类 验证邮箱 验证网址 验证日期 验证手机号 验证IP 验证身份证是否有效 是不是Int型的 看字符串的长度是不是在限定数之间 一个...判断字符串是否为数字 是否为数字型 验证是否包含汉语 验证是否全部汉语

    .Net小程序-----随机生成器

    用C# Winform做的小程序:随机数生成器;简单易用! 包括的功能有: 1.随机生成纯数字 2.随机生成字符串 ...5.以txt文件形式,保存所生成的纯数字/字符串/序列号/手机号 6.支持由用户填写纯数字/字符串的位数

    手机号抽奖程序分享.zip

    C#开发的,winform,之前也是下载回来,自己改良过,可以用于抽手机号。抽奖结果的手机号中间四位数用星号替换了字符串。

    linxl-address:将一个包含姓名、地址和手机电话号码的字符串拆分开来

    linxlAddress将一个包含姓名、地址和手机/电话号码的字符串拆分开来。base [removed][removed] [removed] console.log(linxlAddress('18300000000赵三广东广州天河区林和中路11号')) [removed]npmnpm install ...

    C#编程经验技巧宝典

    79 &lt;br&gt;0115 如何判断是否为数字 79 &lt;br&gt;0116 如何在字符串中查找指定字符 79 &lt;br&gt;0117 如何在字符串中用一子串替换另一子串 80 &lt;br&gt;0118 将新字符串添加到已有字符串中 80 &lt;br&gt;0119 如何在...

    C#封装的Utilities基础类库.rar

    支持C#语言,封装了json操作,xml操作,字符串操作,验证码操作,压缩解压缩操作,文件操作,验证数字、邮箱、身份证、手机号等操作,网络操作等等。

    C#开发实例大全(基础卷).软件开发技术联盟(带详细书签) PDF 下载

    全书分6篇共25章,主要内容有C#开发环境的使用、C#语言基础应用、字符串处理技术、数组和集合的使用、面向对象编程技术、数据结构与算法、Windows窗体基础、特色窗体界面、窗体控制技术、MDI窗体和继承窗体、Windows...

    C#程序开发范例宝典(第2版).part02

    实例229 C#中实现文件拖放 317 实例230 文件比较 318 实例231 获取文件夹中的图标资源 319 实例232 获取文件夹下的所有文件夹及文件的名称 321 第7章 操作系统与Windows相关程序 324 7.1 启动相关 325 实例...

    C#经验技巧宝典6-10.rar

    0203 如何使用正则表达式验证手机号 122 0204 如何使用正则表达式验证身份证号 122 0205 如何使用正则表达式验证两位小数 122 0206 如何使用正则表达式验证一年的12个月份 123 0207 如何使用正则表达式...

    asp.net旅游网站(源码+数据库+文档+配置文档).rar

    2、数据库连接字符串在App_Code文件夹下的DB.cs文件中; 3、修改DB.cs文件中的变量:cnstr;修改为符合您计算机的格式即可; cnstr = "Data Source=LB-201210161711\\A;Initial Catalog=TourDB;Integrated Security=...

    RegexHelper.cs

    正则表达式验证是否手机号码,验证是否IP,取仅保留中文、英文、数字的字符串,替换各种字符串

    手机号码归属地查询工具 1.0

    手机号码归属地查询工具可以查询手机号码归属地、运营商、归属地区号、区段、数据库ID等信息,需要一定的C#基础。菜单功能1、输入想要查询的手机号码,点击[查询]按钮2、下方会根据区段、归属地、运行商、归属地区号...

    手机号码归属地查询软件

    这是一个运用数据库来实现的手机号码归属地的查询软件 ,大家可以自己下用数据库建立一个表格,各列的属性看代码,之后用我的提供的数据源...最后在App.config文件中修改自己的数据库连接字符串,之后就可以运行了。

    MUR号码归属地查询源码

    一、源码特点 可以通过本源码熟悉号码归属地查询的方法!需要一定的C#基础 二、菜单功能 1、输入想要查询的手机号码,点击[查询]按钮 2、下方会根据区段、归属... 2、默认数据库连接字符串在webconfig配置文件中修改

    MySQL使用正则表达式进行查询操作经典实例总结

    字符“.”代替字符串中的任意一个字符 SELECT * FROM fruits WHERE f_name REGEXP 'a.g'; 星号“*”匹配前面的字符任意多次,包括0次。加号“+”匹配前面的字符至少一次 SELECT * FROM fruits WHERE

Global site tag (gtag.js) - Google Analytics