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 = ; i < IPs.Length; i++)
{
if (!regex.IsMatch(IPs[i]))
{
return false;
}
if (Convert.ToUInt16(IPs[i]) > )
{
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 > )
{
return false;
}
int count = MetarnetRegex.GetStringCount(input, "::");
if (count > )
{
return false;
}
else if (count == )
{
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);
} }
/* *******************************************************************
252 * 1、通过“:”来分割字符串看得到的字符串数组长度是否小于等于8
253 * 2、判断输入的IPV6字符串中是否有“::”。
254 * 3、如果没有“::”采用 ^([\da-f]{1,4}:){7}[\da-f]{1,4}$ 来判断
255 * 4、如果有“::” ,判断"::"是否止出现一次
256 * 5、如果出现一次以上 返回false
257 * 6、^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$
258 * ******************************************************************/
/// <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 != -)
{
return + GetStringCount(input.Substring(index + compare.Length), compare);
}
else
{
return ;
} }
}
}

												

C# 正则表达式 判断各种字符串(如手机号)的更多相关文章

  1. boolean matches(String regex)正则表达式判断当前字符串是否满足格式要求

    package seday02;/*** boolean matches(String regex) * 使用给定正则表达式判断当前字符串是否满足格式要求,满足 则返回true. * 注意:此方法是做 ...

  2. 在Java中用正则表达式判断一个字符串是否是数字的方法

    package chengyujia; import java.util.regex.Pattern; public class NumberUtil { /** * 判断一个字符串是否是数字. * ...

  3. js正则表达式判断一个字符串是否是正确的有数字和小数点组成的金钱形式和 判读数值类型的正则表达式

    function checkRates(str){    var re = /^(([1-9][0-9]*\.[0-9][0-9]*)|([0]\.[0-9][0-9]*)|([1-9][0-9]*) ...

  4. java中用正则表达式判断中文字符串中是否含有英文或者数字

    public static boolean includingNUM(String str)throws  Exception{ Pattern p  = Pattern.compile(" ...

  5. iOS开发-通过正则表达式判断字符串是否为纯阿拉伯数字

    iOS开发-通过正则表达式判断字符串是否为纯阿拉伯数字 简述:NSString * regex_0 = @"\\d{1,}";   /*允许首位为0*/ NSString * re ...

  6. C# 判断一字符串是否为合法数字(正则表达式)

    判断一个字符串是否为合法整数(不限制长度) public static bool IsInteger(string s) { string pattern = @"^\d*$"; ...

  7. VFP正则表达式判断是否是手机号码/电子邮件

    正则表达式,可以理解为字符匹配或搜索技术 ,重要的是Pattern属性的写法. *--判断是否是手机号码Function isMobiPhoneLparameters cStroRegExp=Newo ...

  8. java中判断一个字符串是否“都为数字”和“是否包含数字”和“截取数字”

    在javascript中有一个方法isDigit()使用来判断一个字符串是否都是数字,在java的字符串处理方法中没有这样的方法,觉得常常需要用到,于是上网搜了一下,整理出了两个用正则表达式匹配的判断 ...

  9. JAVA 判断一个字符串是不是一个合法的日期格式

    原文:http://www.cnblogs.com/xdp-gacl/p/3548307.html 最近开发公司的项目,一直找不到合适的正则表达式可以判断一个字符串是否可以转成日期,今天发现可以采用S ...

随机推荐

  1. SSH 远程端口转发

    既然"本地端口转发"是指绑定本地端口的转发,那么"远程端口转发"(remote forwarding)当然是指绑定远程端口的转发. 还是接着看上面那个例子,ho ...

  2. spring boot 学习(二)spring boot 框架整合 thymeleaf

    spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...

  3. vs2012 怎样解决 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包的问题

    今天用vs2012打开项目总是报这个错误,在网上找到解决方法 1.问题描述: 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPac ...

  4. springboot2.0 web 开发标准目录架构

    ├── clean-run.sh ├── logs/ 日志文件目录 │ ├── sb2-web_test_2018-06-02_0959.0.log │ └── sb2-web_test.log | ...

  5. restful 初探

    1.restful 是一种编程规范,能够实现现在丰富的客户端(安卓,ios,桌面等)平等的访问服务器提供的服务. 2.重要的是利用restful来设计实现 符合该编程规范的api.

  6. C++设计模式之解释器模式

    2013年07月06日 19:43:00 阅读数:8853 概述: 未来机器智能化已然成为趋势,现在手机都能听懂英语和普通话,那我大中华几万种方言的被智能化也许也是趋势,我们的方言虽然和普通话相似,但 ...

  7. 玩转X-CTR100 l STM32F4 l W25Q64 SPI串行FLASH存储

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      本文介绍X-CTR100控制器 板载FLA ...

  8. mysql-5.6.17-win32安装

    下载免安装压缩文件http://dev.mysql.com/downloads/mysql/ 解压到自定义目录,我这里演示的是D:\wamp\mysql\   复制根目录下的my-default.in ...

  9. WebGL编程指南案例解析之多数据存储于一个缓冲区以及着色器通信

    //顶点着色器往片元着色器传值 //多个参数值存于一个缓冲对象中 var vShader = ` attribute vec4 a_Position; attribute float a_PointS ...

  10. windows下perl的安装和脚本的运行

    参考 1.windows下perl的安装和脚本的运行: 2.fddb测试fddb的评估方法: 3.gunplot5.2.4-download: 完