当前位置: 首页 > news >正文

老外写的正则表达式的类

又是在 www.asp.net上找到的.因为其很简单并且作者也是直接了当把代码拿上来了,所以就不翻译了
代码如下

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Text.RegularExpressions;
None.gif
None.gif
namespace  CoreWebLibrary.Text.RegularExpressions
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// A utility class containing frequently used Regular Expression Patterns
InBlock.gif    
/// and two utility methods to see if a passed string conforms to the designated 
InBlock.gif    
/// pattern.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Patterns
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
InBlock.gif      
public const string EMAIL = @"^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$" ;
InBlock.gif
InBlock.gif      
public const string US_ZIPCODE = @"^\d{5}$" ;
InBlock.gif      
public const string US_ZIPCODE_PLUS_FOUR = @"^\d{5}((-|\s)?\d{4})$";
InBlock.gif      
public const string US_ZIPCODE_PLUS_FOUR_OPTIONAL = @"^\d{5}((-|\s)?\d{4})?$" ;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// Permissive US Telephone Regex. Does not allow extensions.
InBlock.gif      
/// </summary>
InBlock.gif      
/// <example>
InBlock.gif      
/// Allows: 324-234-3433, 3242343434, (234)234-234, (234) 234-2343
ExpandedSubBlockEnd.gif      
/// </example>

InBlock.gif      public const string US_TELEPHONE = @"^([\(]{1}[0-9]{3}[\)]{1}[\.| |\-]{0,1}|^[0-9]{3}[\.|\-| ]?)?[0-9]{3}(\.|\-| )?[0-9]{4}$";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// This matches a url in the generic format 
InBlock.gif      
/// scheme://authority/path?query#fragment
InBlock.gif      
/// </summary>
InBlock.gif      
/// <example>
InBlock.gif      
/// Allows: http://www.yahoo.comhttps://www.yahoo.com, ftp://www.yahoo.com
ExpandedSubBlockEnd.gif      
/// </example>

InBlock.gif      public const string URL = @"^(?<Protocol>\w+):\/\/(?<Domain>[\w.]+\/?)\S*$";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// This matches an ip address in the format xxx-xxx-xxx-xxx
InBlock.gif      
/// each group of xxx must be less than or equal to 255
InBlock.gif      
/// </summary>
InBlock.gif      
/// <example>
InBlock.gif      
/// Allows: 123.123.123.123, 192.168.1.1
ExpandedSubBlockEnd.gif      
/// </example>

InBlock.gif      public const string IP_ADDRESS = @"^(?<First>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Second>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Third>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Fourth>2[0-4]\d|25[0-5]|[01]?\d\d?)$";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// This matches a date in the format mm/dd/yy
InBlock.gif      
/// </summary>
InBlock.gif      
/// <example>
InBlock.gif      
/// Allows: 01/05/05, 12/30/99, 04/11/05
InBlock.gif      
/// Does not allow: 01/05/2000, 2/2/02
ExpandedSubBlockEnd.gif      
/// </example> 

InBlock.gif      public const string DATE_MM_DD_YY = @"^(1[0-2]|0[1-9])/(([1-2][0-9]|3[0-1]|0[1-9])/\d\d)$"
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// This matches a date in the format mm/yy
InBlock.gif      
/// </summary>
InBlock.gif      
/// <example>
InBlock.gif      
/// Allows: 01/05, 11/05, 04/99
InBlock.gif      
/// Does not allow: 1/05, 13/05, 00/05
ExpandedSubBlockEnd.gif      
/// </example>

InBlock.gif      public const string DATE_MM_YY = @"^((0[1-9])|(1[0-2]))\/(\d{2})$";
InBlock.gif
InBlock.gif      
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
///     This matches only numbers(no decimals)
InBlock.gif      
/// </summary>
InBlock.gif      
/// <example>
InBlock.gif      
/// Allows: 0, 1, 123, 4232323, 1212322
ExpandedSubBlockEnd.gif      
/// </example>

InBlock.gif      public const string IS_NUMBER_ONLY = @"^([1-9]\d*)$|^0$";
InBlock.gif     
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// This matches any string with only alpha characters upper or lower case(A-Z)
InBlock.gif      
/// </summary>
InBlock.gif      
/// <example>
InBlock.gif      
/// Allows: abc, ABC, abCd, AbCd
InBlock.gif      
/// Does not allow: A C, abc!, (a,b)
ExpandedSubBlockEnd.gif      
/// </example>

InBlock.gif      public const string IS_ALPHA_ONLY = @"^[a-zA-Z]+$"
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// This matches any string with only upper case alpha character(A-Z)
ExpandedSubBlockEnd.gif      
/// </summary>

InBlock.gif      public const string IS_UPPER_CASE = @"^[A-Z]+$";
InBlock.gif      
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// This matches any string with only lower case alpha character(A-Z)
ExpandedSubBlockEnd.gif      
/// </summary>

InBlock.gif      public const string IS_LOWER_CASE = @"^[a-z]+$";
InBlock.gif      
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// Ensures the string only contains alpha-numeric characters, and
InBlock.gif      
/// not punctuation, spaces, line breaks, etc.
InBlock.gif      
/// </summary>
InBlock.gif      
/// <example>
InBlock.gif      
/// Allows: ab2c, 112ABC, ab23Cd
InBlock.gif      
/// Does not allow: A C, abc!, a.a
ExpandedSubBlockEnd.gif      
/// </example>

InBlock.gif      public const string IS_ALPHA_NUMBER_ONLY = @"^[a-zA-Z0-9]+$";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// Validates US Currency.  Requires $ sign
InBlock.gif      
/// Allows for optional commas and decimal. 
InBlock.gif      
/// No leading zeros. 
InBlock.gif      
/// </summary>
InBlock.gif      
/// <example>Allows: $100,000 or $10000.00 or $10.00 or $.10 or $0 or $0.00
ExpandedSubBlockEnd.gif      
/// Does not allow: $0.10 or 10.00 or 10,000</example>

InBlock.gif      public const string IS_US_CURRENCY = @"^\$(([1-9]\d*|([1-9]\d{0,2}(\,\d{3})*))(\.\d{1,2})?|(\.\d{1,2}))$|^\$[0](.00)?$";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// Matches major credit cards including: Visa (length 16, prefix 4); 
InBlock.gif      
/// Mastercard (length 16, prefix 51-55);
InBlock.gif      
/// Diners Club/Carte Blanche (length 14, prefix 36, 38, or 300-305); 
InBlock.gif      
/// Discover (length 16, prefix 6011); 
InBlock.gif      
/// American Express (length 15, prefix 34 or 37). 
InBlock.gif      
/// Saves the card type as a named group to facilitate further validation 
InBlock.gif      
/// against a "card type" checkbox in a program. 
InBlock.gif      
/// All 16 digit formats are grouped 4-4-4-4 with an optional hyphen or space 
InBlock.gif      
/// between each group of 4 digits. 
InBlock.gif      
/// The American Express format is grouped 4-6-5 with an optional hyphen or space 
InBlock.gif      
/// between each group of digits. 
InBlock.gif      
/// Formatting characters must be consistant, i.e. if two groups are separated by a hyphen, 
InBlock.gif      
/// all groups must be separated by a hyphen for a match to occur.
ExpandedSubBlockEnd.gif      
/// </summary>

InBlock.gif      public const string CREDIT_CARD = @"^(?:(?<Visa>4\d{3})|(?<Mastercard>5[1-5]\d{2})|(?<Discover>6011)|(?<DinersClub>(?:3[68]\d{2})|(?:30[0-5]\d))|(?<AmericanExpress>3[47]\d{2}))([ -]?)(?(DinersClub)(?:\d{6}\1\d{4})|(?(AmericanExpress)(?:\d{6}\1\d{5})|(?:\d{4}\1\d{4}\1\d{4})))$";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// Matches social security in the following format xxx-xx-xxxx
InBlock.gif      
/// where x is a number
InBlock.gif      
/// </summary>
InBlock.gif      
/// <example>
InBlock.gif      
/// Allows: 123-45-6789, 232-432-1212
ExpandedSubBlockEnd.gif      
/// </example>

InBlock.gif      public const string SOCIAL_SECURITY = @"^\d{3}-\d{2}-\d{4}$";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// Matches x,x where x is a name, spaces are only allowed between comma and name
InBlock.gif      
/// </summary>
InBlock.gif      
/// <example>
InBlock.gif      
/// Allows: christophersen,eric; christophersen, eric
InBlock.gif      
/// Not allowed: christophersen ,eric;
ExpandedSubBlockEnd.gif      
/// </example>

InBlock.gif      public const string NAME_COMMA_NAME = @"^[a-zA-Z]+,\s?[a-zA-Z]+$";
InBlock.gif        
InBlock.gif      
private Patterns()
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockEnd.gif      }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// Checks to see if the passed input has the passed pattern
InBlock.gif      
/// </summary>
InBlock.gif      
/// <param name="pattern">The pattern to use</param>
InBlock.gif      
/// <param name="input">The input to check</param>
ExpandedSubBlockEnd.gif      
/// <returns>True if the input has the pattern, false otherwise</returns>

InBlock.gif      public static bool HasPattern( string pattern, string input )
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         Regex regEx 
= new Regex(pattern);
InBlock.gif         
return regEx.IsMatch(input);
ExpandedSubBlockEnd.gif      }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// Checks the passed input to make sure it has all the patterns in the 
InBlock.gif      
/// passed patterns array
InBlock.gif      
/// </summary>
InBlock.gif      
/// <param name="patterns">Array of patterns</param>
InBlock.gif      
/// <param name="input">String value to check</param>
ExpandedSubBlockEnd.gif      
/// <returns>True if the input has all of the patterns, false otherwise.</returns>

InBlock.gif      public static bool HasPatterns(string[] patterns, string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         
for (int i = 0; i < patterns.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif          
if (Patterns.HasPattern(patterns[i], input) == false)
InBlock.gif              
return false;
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif         
return true;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif

转载于:https://www.cnblogs.com/aspnetx/archive/2006/10/01/519875.html

相关文章:

  • Cookie和Session
  • 浙江金华 图论整理
  • [转]成功创业家的心理
  • linux安装jdk
  • 经典英语摘录
  • Message Loop 原理及应用
  • JVM内存区域划分
  • [转载]我的个人信息雷达——580k使用手记
  • LeetCode:平衡二叉树【110】
  • 常用的shell脚本
  • XPath基础语法(1)
  • P1273 有线电视网
  • DotText源码阅读(2)-工程、数据库表结构
  • 23 Java学习之RandomAccessFile
  • 在本地机上还复在另一台机器上备份的数据库
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • 5、React组件事件详解
  • java8 Stream Pipelines 浅析
  • JavaScript工作原理(五):深入了解WebSockets,HTTP/2和SSE,以及如何选择
  • JDK 6和JDK 7中的substring()方法
  • Linux下的乱码问题
  • Rancher如何对接Ceph-RBD块存储
  • Redis在Web项目中的应用与实践
  • SwizzleMethod 黑魔法
  • TypeScript迭代器
  • Vue.js源码(2):初探List Rendering
  • 爱情 北京女病人
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 手机端车牌号码键盘的vue组件
  • 无服务器化是企业 IT 架构的未来吗?
  • 赢得Docker挑战最佳实践
  • 与 ConTeXt MkIV 官方文档的接驳
  • Spring第一个helloWorld
  • 容器镜像
  • #Ubuntu(修改root信息)
  • $.extend({},旧的,新的);合并对象,后面的覆盖前面的
  • (175)FPGA门控时钟技术
  • (4) PIVOT 和 UPIVOT 的使用
  • (C语言)共用体union的用法举例
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (Matalb时序预测)WOA-BP鲸鱼算法优化BP神经网络的多维时序回归预测
  • (react踩过的坑)antd 如何同时获取一个select 的value和 label值
  • (排序详解之 堆排序)
  • (算法二)滑动窗口
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .form文件_一篇文章学会文件上传
  • .NET CF命令行调试器MDbg入门(二) 设备模拟器
  • .net wcf memory gates checking failed
  • .NET 简介:跨平台、开源、高性能的开发平台
  • .net 怎么循环得到数组里的值_关于js数组
  • .NET3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke(转)