正则表达式的应用类 [代码分享]

  1 None.gif using  System;
  2 None.gif using  System.Collections.Generic;
  3 None.gif using  System.Text;
  4 None.gif using  System.Text.RegularExpressions;
  5 None.gif
  6 None.gif namespace  CoreWebLibrary.Text.RegularExpressions
  7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**//**//// <summary>
  9InBlock.gif    /// A utility class containing frequently used Regular Expression Patterns
 10InBlock.gif    /// and two utility methods to see if a passed string conforms to the designated 
 11InBlock.gif    /// pattern.
 12ExpandedSubBlockEnd.gif    /// </summary>

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

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

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

 46InBlock.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?)$";
 47InBlock.gif
 48ExpandedSubBlockStart.gifContractedSubBlock.gif      /**//**//**//// <summary>
 49InBlock.gif      /// This matches a date in the format mm/dd/yy
 50InBlock.gif      /// </summary>
 51InBlock.gif      /// <example>
 52InBlock.gif      /// Allows: 01/05/05, 12/30/99, 04/11/05
 53InBlock.gif      /// Does not allow: 01/05/2000, 2/2/02
 54ExpandedSubBlockEnd.gif      /// </example> 

 55InBlock.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)$"
 56InBlock.gif
 57InBlock.gif
 58ExpandedSubBlockStart.gifContractedSubBlock.gif      /**//**//**//// <summary>
 59InBlock.gif      /// This matches a date in the format mm/yy
 60InBlock.gif      /// </summary>
 61InBlock.gif      /// <example>
 62InBlock.gif      /// Allows: 01/05, 11/05, 04/99
 63InBlock.gif      /// Does not allow: 1/05, 13/05, 00/05
 64ExpandedSubBlockEnd.gif      /// </example>

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

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

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

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

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

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

112InBlock.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)?$";
113InBlock.gif
114ExpandedSubBlockStart.gifContractedSubBlock.gif      /**//**//**//// <summary>
115InBlock.gif      /// Matches major credit cards including: Visa (length 16, prefix 4); 
116InBlock.gif      /// Mastercard (length 16, prefix 51-55);
117InBlock.gif      /// Diners Club/Carte Blanche (length 14, prefix 36, 38, or 300-305); 
118InBlock.gif      /// Discover (length 16, prefix 6011); 
119InBlock.gif      /// American Express (length 15, prefix 34 or 37). 
120InBlock.gif      /// Saves the card type as a named group to facilitate further validation 
121InBlock.gif      /// against a "card type" checkbox in a program. 
122InBlock.gif      /// All 16 digit formats are grouped 4-4-4-4 with an optional hyphen or space 
123InBlock.gif      /// between each group of 4 digits. 
124InBlock.gif      /// The American Express format is grouped 4-6-5 with an optional hyphen or space 
125InBlock.gif      /// between each group of digits. 
126InBlock.gif      /// Formatting characters must be consistant, i.e. if two groups are separated by a hyphen, 
127InBlock.gif      /// all groups must be separated by a hyphen for a match to occur.
128ExpandedSubBlockEnd.gif      /// </summary>

129InBlock.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})))$";
130InBlock.gif
131ExpandedSubBlockStart.gifContractedSubBlock.gif      /**//**//**//// <summary>
132InBlock.gif      /// Matches social security in the following format xxx-xx-xxxx
133InBlock.gif      /// where x is a number
134InBlock.gif      /// </summary>
135InBlock.gif      /// <example>
136InBlock.gif      /// Allows: 123-45-6789, 232-432-1212
137ExpandedSubBlockEnd.gif      /// </example>

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

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

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

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

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

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

179InBlock.gif
180InBlock.gif         return true;
181ExpandedSubBlockEnd.gif      }

182ExpandedSubBlockEnd.gif    }

183InBlock.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值