正则表达式的应用类 [代码分享]
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Text.RegularExpressions;
5
6namespace CoreWebLibrary.Text.RegularExpressions
7{
8 /**//// <summary>
9 /// A utility class containing frequently used Regular Expression Patterns
10 /// and two utility methods to see if a passed string conforms to the designated
11 /// pattern.
12 /// </summary>
13 public class Patterns
14 {
15
16 public const string EMAIL = @"^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$" ;
17
18 public const string US_ZIPCODE = @"^\d{5}$" ;
19 public const string US_ZIPCODE_PLUS_FOUR = @"^\d{5}((-|\s)?\d{4})$";
20 public const string US_ZIPCODE_PLUS_FOUR_OPTIONAL = @"^\d{5}((-|\s)?\d{4})?$" ;
21
22 /**//// <summary>
23 /// Permissive US Telephone Regex. Does not allow extensions.
24 /// </summary>
25 /// <example>
26 /// Allows: 324-234-3433, 3242343434, (234)234-234, (234) 234-2343
27 /// </example>
28 public const string US_TELEPHONE = @"^([\(]{1}[0-9]{3}[\)]{1}[\.| |\-]{0,1}|^[0-9]{3}[\.|\-| ]?)?[0-9]{3}(\.|\-| )?[0-9]{4}$";
29
30 /**//// <summary>
31 /// This matches a url in the generic format
32 /// scheme://authority/path?query#fragment
33 /// </summary>
34 /// <example>
35 /// Allows: http://www.yahoo.com, https://www.yahoo.com, ftp://www.yahoo.com
36 /// </example>
37 public const string URL = @"^(?<Protocol>\w+):\/\/(?<Domain>[\w.]+\/?)\S*$";
38
39 /**//// <summary>
40 /// This matches an ip address in the format xxx-xxx-xxx-xxx
41 /// each group of xxx must be less than or equal to 255
42 /// </summary>
43 /// <example>
44 /// Allows: 123.123.123.123, 192.168.1.1
45 /// </example>
46 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?)$";
47
48 /**//// <summary>
49 /// This matches a date in the format mm/dd/yy
50 /// </summary>
51 /// <example>
52 /// Allows: 01/05/05, 12/30/99, 04/11/05
53 /// Does not allow: 01/05/2000, 2/2/02
54 /// </example>
55 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)$";
56
57
58 /**//// <summary>
59 /// This matches a date in the format mm/yy
60 /// </summary>
61 /// <example>
62 /// Allows: 01/05, 11/05, 04/99
63 /// Does not allow: 1/05, 13/05, 00/05
64 /// </example>
65 public const string DATE_MM_YY = @"^((0[1-9])|(1[0-2]))\/(\d{2})$";
66
67
68 /**//// <summary>
69 /// This matches only numbers(no decimals)
70 /// </summary>
71 /// <example>
72 /// Allows: 0, 1, 123, 4232323, 1212322
73 /// </example>
74 public const string IS_NUMBER_ONLY = @"^([1-9]\d*)$|^0$";
75
76 /**//// <summary>
77 /// This matches any string with only alpha characters upper or lower case(A-Z)
78 /// </summary>
79 /// <example>
80 /// Allows: abc, ABC, abCd, AbCd
81 /// Does not allow: A C, abc!, (a,b)
82 /// </example>
83 public const string IS_ALPHA_ONLY = @"^[a-zA-Z]+$";
84
85 /**//// <summary>
86 /// This matches any string with only upper case alpha character(A-Z)
87 /// </summary>
88 public const string IS_UPPER_CASE = @"^[A-Z]+$";
89
90 /**//// <summary>
91 /// This matches any string with only lower case alpha character(A-Z)
92 /// </summary>
93 public const string IS_LOWER_CASE = @"^[a-z]+$";
94
95 /**//// <summary>
96 /// Ensures the string only contains alpha-numeric characters, and
97 /// not punctuation, spaces, line breaks, etc.
98 /// </summary>
99 /// <example>
100 /// Allows: ab2c, 112ABC, ab23Cd
101 /// Does not allow: A C, abc!, a.a
102 /// </example>
103 public const string IS_ALPHA_NUMBER_ONLY = @"^[a-zA-Z0-9]+$";
104
105 /**//// <summary>
106 /// Validates US Currency. Requires $ sign
107 /// Allows for optional commas and decimal.
108 /// No leading zeros.
109 /// </summary>
110 /// <example>Allows: $100,000 or $10000.00 or $10.00 or $.10 or $0 or $0.00
111 /// Does not allow: $0.10 or 10.00 or 10,000</example>
112 public const string IS_US_CURRENCY = @"^\$(([1-9]\d*|([1-9]\d{0,2}(\,\d{3})*))(\.\d{1,2})?|(\.\d{1,2}))$|^\$[0](.00)?$";
113
114 /**//// <summary>
115 /// Matches major credit cards including: Visa (length 16, prefix 4);
116 /// Mastercard (length 16, prefix 51-55);
117 /// Diners Club/Carte Blanche (length 14, prefix 36, 38, or 300-305);
118 /// Discover (length 16, prefix 6011);
119 /// American Express (length 15, prefix 34 or 37).
120 /// Saves the card type as a named group to facilitate further validation
121 /// against a "card type" checkbox in a program.
122 /// All 16 digit formats are grouped 4-4-4-4 with an optional hyphen or space
123 /// between each group of 4 digits.
124 /// The American Express format is grouped 4-6-5 with an optional hyphen or space
125 /// between each group of digits.
126 /// Formatting characters must be consistant, i.e. if two groups are separated by a hyphen,
127 /// all groups must be separated by a hyphen for a match to occur.
128 /// </summary>
129 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})))$";
130
131 /**//// <summary>
132 /// Matches social security in the following format xxx-xx-xxxx
133 /// where x is a number
134 /// </summary>
135 /// <example>
136 /// Allows: 123-45-6789, 232-432-1212
137 /// </example>
138 public const string SOCIAL_SECURITY = @"^\d{3}-\d{2}-\d{4}$";
139
140 /**//// <summary>
141 /// Matches x,x where x is a name, spaces are only allowed between comma and name
142 /// </summary>
143 /// <example>
144 /// Allows: christophersen,eric; christophersen, eric
145 /// Not allowed: christophersen ,eric;
146 /// </example>
147 public const string NAME_COMMA_NAME = @"^[a-zA-Z]+,\s?[a-zA-Z]+$";
148
149 private Patterns()
150 {
151 }
152
153 /**//// <summary>
154 /// Checks to see if the passed input has the passed pattern
155 /// </summary>
156 /// <param name="pattern">The pattern to use</param>
157 /// <param name="input">The input to check</param>
158 /// <returns>True if the input has the pattern, false otherwise</returns>
159 public static bool HasPattern( string pattern, string input )
160 {
161 Regex regEx = new Regex(pattern);
162 return regEx.IsMatch(input);
163 }
164
165 /**//// <summary>
166 /// Checks the passed input to make sure it has all the patterns in the
167 /// passed patterns array
168 /// </summary>
169 /// <param name="patterns">Array of patterns</param>
170 /// <param name="input">String value to check</param>
171 /// <returns>True if the input has all of the patterns, false otherwise.</returns>
172 public static bool HasPatterns(string[] patterns, string input)
173 {
174 for (int i = 0; i < patterns.Length; i++)
175 {
176 if (Patterns.HasPattern(patterns[i], input) == false)
177 return false;
178 }
179
180 return true;
181 }
182 }
183
2using System.Collections.Generic;
3using System.Text;
4using System.Text.RegularExpressions;
5
6namespace CoreWebLibrary.Text.RegularExpressions
7{
8 /**//// <summary>
9 /// A utility class containing frequently used Regular Expression Patterns
10 /// and two utility methods to see if a passed string conforms to the designated
11 /// pattern.
12 /// </summary>
13 public class Patterns
14 {
15
16 public const string EMAIL = @"^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$" ;
17
18 public const string US_ZIPCODE = @"^\d{5}$" ;
19 public const string US_ZIPCODE_PLUS_FOUR = @"^\d{5}((-|\s)?\d{4})$";
20 public const string US_ZIPCODE_PLUS_FOUR_OPTIONAL = @"^\d{5}((-|\s)?\d{4})?$" ;
21
22 /**//// <summary>
23 /// Permissive US Telephone Regex. Does not allow extensions.
24 /// </summary>
25 /// <example>
26 /// Allows: 324-234-3433, 3242343434, (234)234-234, (234) 234-2343
27 /// </example>
28 public const string US_TELEPHONE = @"^([\(]{1}[0-9]{3}[\)]{1}[\.| |\-]{0,1}|^[0-9]{3}[\.|\-| ]?)?[0-9]{3}(\.|\-| )?[0-9]{4}$";
29
30 /**//// <summary>
31 /// This matches a url in the generic format
32 /// scheme://authority/path?query#fragment
33 /// </summary>
34 /// <example>
35 /// Allows: http://www.yahoo.com, https://www.yahoo.com, ftp://www.yahoo.com
36 /// </example>
37 public const string URL = @"^(?<Protocol>\w+):\/\/(?<Domain>[\w.]+\/?)\S*$";
38
39 /**//// <summary>
40 /// This matches an ip address in the format xxx-xxx-xxx-xxx
41 /// each group of xxx must be less than or equal to 255
42 /// </summary>
43 /// <example>
44 /// Allows: 123.123.123.123, 192.168.1.1
45 /// </example>
46 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?)$";
47
48 /**//// <summary>
49 /// This matches a date in the format mm/dd/yy
50 /// </summary>
51 /// <example>
52 /// Allows: 01/05/05, 12/30/99, 04/11/05
53 /// Does not allow: 01/05/2000, 2/2/02
54 /// </example>
55 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)$";
56
57
58 /**//// <summary>
59 /// This matches a date in the format mm/yy
60 /// </summary>
61 /// <example>
62 /// Allows: 01/05, 11/05, 04/99
63 /// Does not allow: 1/05, 13/05, 00/05
64 /// </example>
65 public const string DATE_MM_YY = @"^((0[1-9])|(1[0-2]))\/(\d{2})$";
66
67
68 /**//// <summary>
69 /// This matches only numbers(no decimals)
70 /// </summary>
71 /// <example>
72 /// Allows: 0, 1, 123, 4232323, 1212322
73 /// </example>
74 public const string IS_NUMBER_ONLY = @"^([1-9]\d*)$|^0$";
75
76 /**//// <summary>
77 /// This matches any string with only alpha characters upper or lower case(A-Z)
78 /// </summary>
79 /// <example>
80 /// Allows: abc, ABC, abCd, AbCd
81 /// Does not allow: A C, abc!, (a,b)
82 /// </example>
83 public const string IS_ALPHA_ONLY = @"^[a-zA-Z]+$";
84
85 /**//// <summary>
86 /// This matches any string with only upper case alpha character(A-Z)
87 /// </summary>
88 public const string IS_UPPER_CASE = @"^[A-Z]+$";
89
90 /**//// <summary>
91 /// This matches any string with only lower case alpha character(A-Z)
92 /// </summary>
93 public const string IS_LOWER_CASE = @"^[a-z]+$";
94
95 /**//// <summary>
96 /// Ensures the string only contains alpha-numeric characters, and
97 /// not punctuation, spaces, line breaks, etc.
98 /// </summary>
99 /// <example>
100 /// Allows: ab2c, 112ABC, ab23Cd
101 /// Does not allow: A C, abc!, a.a
102 /// </example>
103 public const string IS_ALPHA_NUMBER_ONLY = @"^[a-zA-Z0-9]+$";
104
105 /**//// <summary>
106 /// Validates US Currency. Requires $ sign
107 /// Allows for optional commas and decimal.
108 /// No leading zeros.
109 /// </summary>
110 /// <example>Allows: $100,000 or $10000.00 or $10.00 or $.10 or $0 or $0.00
111 /// Does not allow: $0.10 or 10.00 or 10,000</example>
112 public const string IS_US_CURRENCY = @"^\$(([1-9]\d*|([1-9]\d{0,2}(\,\d{3})*))(\.\d{1,2})?|(\.\d{1,2}))$|^\$[0](.00)?$";
113
114 /**//// <summary>
115 /// Matches major credit cards including: Visa (length 16, prefix 4);
116 /// Mastercard (length 16, prefix 51-55);
117 /// Diners Club/Carte Blanche (length 14, prefix 36, 38, or 300-305);
118 /// Discover (length 16, prefix 6011);
119 /// American Express (length 15, prefix 34 or 37).
120 /// Saves the card type as a named group to facilitate further validation
121 /// against a "card type" checkbox in a program.
122 /// All 16 digit formats are grouped 4-4-4-4 with an optional hyphen or space
123 /// between each group of 4 digits.
124 /// The American Express format is grouped 4-6-5 with an optional hyphen or space
125 /// between each group of digits.
126 /// Formatting characters must be consistant, i.e. if two groups are separated by a hyphen,
127 /// all groups must be separated by a hyphen for a match to occur.
128 /// </summary>
129 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})))$";
130
131 /**//// <summary>
132 /// Matches social security in the following format xxx-xx-xxxx
133 /// where x is a number
134 /// </summary>
135 /// <example>
136 /// Allows: 123-45-6789, 232-432-1212
137 /// </example>
138 public const string SOCIAL_SECURITY = @"^\d{3}-\d{2}-\d{4}$";
139
140 /**//// <summary>
141 /// Matches x,x where x is a name, spaces are only allowed between comma and name
142 /// </summary>
143 /// <example>
144 /// Allows: christophersen,eric; christophersen, eric
145 /// Not allowed: christophersen ,eric;
146 /// </example>
147 public const string NAME_COMMA_NAME = @"^[a-zA-Z]+,\s?[a-zA-Z]+$";
148
149 private Patterns()
150 {
151 }
152
153 /**//// <summary>
154 /// Checks to see if the passed input has the passed pattern
155 /// </summary>
156 /// <param name="pattern">The pattern to use</param>
157 /// <param name="input">The input to check</param>
158 /// <returns>True if the input has the pattern, false otherwise</returns>
159 public static bool HasPattern( string pattern, string input )
160 {
161 Regex regEx = new Regex(pattern);
162 return regEx.IsMatch(input);
163 }
164
165 /**//// <summary>
166 /// Checks the passed input to make sure it has all the patterns in the
167 /// passed patterns array
168 /// </summary>
169 /// <param name="patterns">Array of patterns</param>
170 /// <param name="input">String value to check</param>
171 /// <returns>True if the input has all of the patterns, false otherwise.</returns>
172 public static bool HasPatterns(string[] patterns, string input)
173 {
174 for (int i = 0; i < patterns.Length; i++)
175 {
176 if (Patterns.HasPattern(patterns[i], input) == false)
177 return false;
178 }
179
180 return true;
181 }
182 }
183