C# Regex正则验证规则
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);
} }
/* *******************************************************************
* 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 != -)
{
return + GetStringCount(input.Substring(index + compare.Length),compare);
}
else
{
return ;
} }
}
}
C# Regex正则验证规则的更多相关文章
- JavaScript输入表单数据正则验证规则
emailNameReg: /^(([a-zA-Z0-9]+\w*((\.\w+)|(-\w+))*[\.-]?[a-zA-Z0-9]+)|([a-zA-Z0-9]))$/, //匹配邮箱名称 ema ...
- easyui扩展正则验证,函数验证
用easyui做业务系统,对于默认的几个验证规则,肯定是不够的,难免会增加几种规则.可是问题来了,往往是我们在开发会遇到很多各种各样的验证,时间久了才发现,这些扩展的正则无非就是添加一个正则验证规则, ...
- iOS身份证的正则验证
在ios项目的开发中可能很多地方都需要用到身份证校验,一般在开发的时候很多人都是直接百度去网上荡相关的正则表达式和校验代码,但是网上疯狂粘贴复制的校验代码本身也可能并不准确,可能会有风险,比如2013 ...
- tp5内置验证规则
验证规则 描述 require 必须验证 alpha 是否为字母 alphaNum 是否为字母和数字 alphaDash 是否为字母.数字,下划线_及破折号- number 是否为数字 integer ...
- yii2验证规则
验证规则 1.内置验证规则 [['sex', 'partner_id'], 'integer'], [['partner_id', 'camp_id',], 'required'], [['creat ...
- TP框架自带的正则验证的规则(转载)
thinkphp框架里面自带有很多自动验证的规则,下面是框架自带的正则验证的规则,官方的说明文档里面没有这么多,所以记下来,以备使用. view sourceprint?01static $regex ...
- TP框架自带的正则验证的规则
thinkphp框架里面自带有很多自动验证的规则,下面是框架自带的正则验证的规则,官方的说明文档里面没有这么多,所以记下来,以备使用. view sourceprint? 01 static $reg ...
- TP5验证规则使用
定义验证器类: namespace app\index\validate; use think\Validate; class User extends Validate { protected $r ...
- TP5验证规则
系统内置的验证规则如下: 格式验证类 require 验证某个字段必须,例如:'name'=>'require' number 或者 integer 验证某个字段的值是否为数字(采用filter ...
随机推荐
- ASP.NET全局异常处理
Web项目部署后,异常直接暴露给用户会产生很不好的体验.只是暴露在服务器端又无法实时记录异常原因以便加以重现并修复.所以配合Log4Net记录日志信息,同时全局异常处理来营造良好用户体验就比较重要了. ...
- canvas入门(一)
canvas是HTML 5中非常重要的一个标签,关于它的功能和使用方法在这我就不一一细谈了,毕竟网上相关知识太多,而且自认为如果是我总结那些知识会让读者看的更乱,所以我就不白费力气了,贴上链接:htt ...
- PHP time() date() strtotime()日期函数总结
日期函数总结—— 一.返回时间戳——若整体数值超出计算机能力范围,返回空. 1. time();返回当前的 Unix 时间戳 例:$a=time(); var_dump($a); //输出:int( ...
- ThinkPHP5专题
TinkPHP5中 1. model中select()后的查询结果是对象而不是数组 2. model中 假如有 $result=$this->field($fields)->where($ ...
- (EXPDP) Fails With Errors ORA-39079 ORA-25306 On One Node In RAC Environment
分类: Oracle DataPump export on one certain RAC instance fails with errors: ORA-39006: internal errorO ...
- [libxml2]_[XML处理]_[使用libxml2的xpath特性修改xml文件内容]
场景: 1.在软件需要保存一些配置项时,使用数据库的话比较复杂,查看内容也不容易.纯文本文件对utf8字符支持也不好. 2.这时候使用xml是最佳选择,使用跨平台库libxml2. 3.基于xpath ...
- 阿里云上到底能运行SAP哪些产品?
本文主要内容大部分来源于SAP已经发布的note: 2552731 - SAP Applications on Alibaba Cloud: Supported Products and IaaS ...
- 【BZOJ3495】PA2010 Riddle
题目大意 有\(n\)个城镇被分成了\(k\)个郡,有\(m\)条连接城镇的无向边.要求给每个郡选择一个城镇作为首都,满足每条边至少有一个端点是首都. 题目分析 每条边至少有一个端点是首都,每个郡至多 ...
- listview加载显示图片
public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> { private ListView lis ...
- 【[NOI2009]植物大战僵尸】
题目 我太\(zz\)了 有一个非常显然的问题就是一个植物显然能保护同一行上比它更靠后的植物,因为显然得先干掉更靠前的植物 首先可以看出来这是一个经典的最大权闭合子图的模型,于是去套最小割 发现植物的 ...