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. stimulsoft report工具—— 简单的多表连接打印报表例子

    一.用报表工具打印多个表格信息(包括学生表.教师表.班级表) 1.准备打印的数据(用sqlserver) 1)班级表

  2. Openwrt TF Card Auto Mount&Check (4)

    1      Scope of Document This document describes tf card bootup auto do file system check when mount ...

  3. java 需要看的书籍

    参考链接:http://www.jianshu.com/p/454fc1e6cbe2 最近要看的有:Effective java  深入理解java 虚拟机  java 并发编程实战 (设计模式的书籍 ...

  4. 第三视角团队:项目UML设计(团队)

    项目UML设计(团队) 团队信息 团队名:第三视角 各成员学号及姓名 姓名 学号 博客链接 张扬(组长) 031602345 http://www.cnblogs.com/sxZhangYang/p/ ...

  5. 使用Setup安装Windows8 RTM方法

    1.下载Windows 8 RTM版. 2.解压ISO映像或加载虚拟光驱. 3.为了安装双系统,打开sources文件夹中的setup.exe才可. 4.打开后选择“在线获取安装程序的最新更新(推荐) ...

  6. 组件的 keep-alive 简介

    本篇文章,我们来讲一下keep-alive的实现.  更容易看懂 Vue中,有三个内置的抽象组件,分别是keep-alive.transition和transition-group, 它们都有一个共同 ...

  7. Android 源码阅读之SMS,MMS

    主界面: com.android.mms.ui.ConversationList.java  [extends ListActivity] 点击新建信息:onListItemClick -〉 posi ...

  8. C高级第三次作业(1)

    6-1 输出月份英文名 1.设计思路: 1.定义一个字符串数组将12个月的英文加进去: 2.判断输入的数是否大于等于1小于等于12: 3.若是 则返还s[n-1]; 4.否则返还NULL: 源代码: ...

  9. CSS的使用

    1.行内样式/内联样式 特点:在具体的HTML标签中引入CSS代码 语法:所有的HTML标签都具有一个style属性,属性值就可以使用CSS样式规则 <标签 style="CSS样式规 ...

  10. int? 竟然真的可以是 null!.NET/C# 确定可空值类型 Nullable 实例的真实类型

    使用 Nullable<T> 我们可以为原本不可能为 null 的值类型像引用类型那样提供一个 null 值.不过注意:Nullable<T> 本身也是个 struct,是个值 ...