正则表达式-----判断各种字符串(如手机号)

  1 using System;
  2 using System.Text.RegularExpressions;
  3 namespace MetarCommonSupport
  4 {
  5     /// <summary>
  6     /// 通过Framwork类库中的Regex类实现了一些特殊功能数据检查
  7     /// </summary>
  8     public class MetarnetRegex
  9     {
 10 
 11         private static MetarnetRegex instance = null;
 12         public static MetarnetRegex GetInstance()
 13         {
 14             if (MetarnetRegex.instance == null)
 15             {
 16                 MetarnetRegex.instance = new MetarnetRegex();
 17             }
 18             return MetarnetRegex.instance;
 19         }
 20         private MetarnetRegex()
 21         {
 22         }
 23         /// <summary>
 24         /// 判断输入的字符串只包含汉字
 25         /// </summary>
 26         /// <param name="input"></param>
 27         /// <returns></returns>
 28         public static bool IsChineseCh(string input)
 29         {
 30             Regex regex = new Regex("^[\u4e00-\u9fa5]+$");
 31             return regex.IsMatch(input);
 32         }
 33         /// <summary>
 34         /// 匹配3位或4位区号的电话号码,其中区号可以用小括号括起来,
 35         /// 也可以不用,区号与本地号间可以用连字号或空格间隔,
 36         /// 也可以没有间隔
 37         /// \(0\d{2}\)[- ]?\d{8}|0\d{2}[- ]?\d{8}|\(0\d{3}\)[- ]?\d{7}|0\d{3}[- ]?\d{7}
 38         /// </summary>
 39         /// <param name="input"></param>
 40         /// <returns></returns>
 41         public static bool IsPhone(string input)
 42         {
 43             string pattern = "^\\(0\\d{2}\\)[- ]?\\d{8}$|^0\\d{2}[- ]?\\d{8}$|^\\(0\\d{3}\\)[- ]?\\d{7}$|^0\\d{3}[- ]?\\d{7}$";
 44             Regex regex = new Regex(pattern);
 45             return regex.IsMatch(input);
 46         }
 47         /// <summary>
 48         /// 判断输入的字符串是否是一个合法的手机号
 49         /// </summary>
 50         /// <param name="input"></param>
 51         /// <returns></returns>
 52         public static bool IsMobilePhone(string input)
 53         {
 54             Regex regex = new Regex("^13\\d{9}$");
 55             return regex.IsMatch(input);
 56 
 57         }
 58 
 59 
 60         /// <summary>
 61         /// 判断输入的字符串只包含数字
 62         /// 可以匹配整数和浮点数
 63         /// ^-?\d+$|^(-?\d+)(\.\d+)?$
 64         /// </summary>
 65         /// <param name="input"></param>
 66         /// <returns></returns>
 67         public static bool IsNumber(string input)
 68         {
 69             string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
 70             Regex regex = new Regex(pattern);
 71             return regex.IsMatch(input);
 72         }
 73         /// <summary>
 74         /// 匹配非负整数
 75         ///
 76         /// </summary>
 77         /// <param name="input"></param>
 78         /// <returns></returns>
 79         public static bool IsNotNagtive(string input)
 80         {
 81             Regex regex = new Regex(@"^\d+$");
 82             return regex.IsMatch(input);
 83         }
 84         /// <summary>
 85         /// 匹配正整数
 86         /// </summary>
 87         /// <param name="input"></param>
 88         /// <returns></returns>
 89         public static bool IsUint(string input)
 90         {
 91             Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
 92             return regex.IsMatch(input);
 93         }
 94         /// <summary>
 95         /// 判断输入的字符串字包含英文字母
 96         /// </summary>
 97         /// <param name="input"></param>
 98         /// <returns></returns>
 99         public static bool IsEnglisCh(string input)
100         {
101             Regex regex = new Regex("^[A-Za-z]+$");
102             return regex.IsMatch(input);
103         }
104 
105 
106         /// <summary>
107         /// 判断输入的字符串是否是一个合法的Email地址
108         /// </summary>
109         /// <param name="input"></param>
110         /// <returns></returns>
111         public static bool IsEmail(string input)
112         {
113             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})(\]?)$";
114             Regex regex = new Regex(pattern);
115             return regex.IsMatch(input);
116         }
117 
118 
119         /// <summary>
120         /// 判断输入的字符串是否只包含数字和英文字母
121         /// </summary>
122         /// <param name="input"></param>
123         /// <returns></returns>
124         public static bool IsNumAndEnCh(string input)
125         {
126             string pattern = @"^[A-Za-z0-9]+$";
127             Regex regex = new Regex(pattern);
128             return regex.IsMatch(input);
129         }
130 
131 
132         /// <summary>
133         /// 判断输入的字符串是否是一个超链接
134         /// </summary>
135         /// <param name="input"></param>
136         /// <returns></returns>
137         public static bool IsURL(string input)
138         {
139             //string pattern = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
140             string pattern = @"^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$";
141             Regex regex = new Regex(pattern);
142             return regex.IsMatch(input);
143         }
144 
145 
146         /// <summary>
147         /// 判断输入的字符串是否是表示一个IP地址
148         /// </summary>
149         /// <param name="input">被比较的字符串</param>
150         /// <returns>是IP地址则为True</returns>
151         public static bool IsIPv4(string input)
152         {
153 
154             string[] IPs = input.Split('.');
155             Regex regex = new Regex(@"^\d+$");
156             for (int i = 0; i < IPs.Length; i++)
157             {
158                 if (!regex.IsMatch(IPs[i]))
159                 {
160                     return false;
161                 }
162                 if (Convert.ToUInt16(IPs[i]) > 255)
163                 {
164                     return false;
165                 }
166             }
167             return true;
168         }
169 
170 
171         /// <summary>
172         /// 计算字符串的字符长度,一个汉字字符将被计算为两个字符
173         /// </summary>
174         /// <param name="input">需要计算的字符串</param>
175         /// <returns>返回字符串的长度</returns>
176         public static int GetCount(string input)
177         {
178             return Regex.Replace(input, @"[\u4e00-\u9fa5/g]", "aa").Length;
179         }
180 
181         /// <summary>
182         /// 调用Regex中IsMatch函数实现一般的正则表达式匹配
183         /// </summary>
184         /// <param name="pattern">要匹配的正则表达式模式。</param>
185         /// <param name="input">要搜索匹配项的字符串</param>
186         /// <returns>如果正则表达式找到匹配项,则为 true;否则,为 false。</returns>
187         public static bool IsMatch(string pattern, string input)
188         {
189             Regex regex = new Regex(pattern);
190             return regex.IsMatch(input);
191         }
192 
193         /// <summary>
194         /// 从输入字符串中的第一个字符开始,用替换字符串替换指定的正则表达式模式的所有匹配项。
195         /// </summary>
196         /// <param name="pattern">模式字符串</param>
197         /// <param name="input">输入字符串</param>
198         /// <param name="replacement">用于替换的字符串</param>
199         /// <returns>返回被替换后的结果</returns>
200         public static string Replace(string pattern, string input, string replacement)
201         {
202             Regex regex = new Regex(pattern);
203             return regex.Replace(input, replacement);
204         }
205 
206         /// <summary>
207         /// 在由正则表达式模式定义的位置拆分输入字符串。
208         /// </summary>
209         /// <param name="pattern">模式字符串</param>
210         /// <param name="input">输入字符串</param>
211         /// <returns></returns>
212         public static string[] Split(string pattern, string input)
213         {
214             Regex regex = new Regex(pattern);
215             return regex.Split(input);
216         }
217         /// <summary>
218         /// 判断输入的字符串是否是合法的IPV6 地址
219         /// </summary>
220         /// <param name="input"></param>
221         /// <returns></returns>
222         public static bool IsIPV6(string input)
223         {
224             string pattern = "";
225             string temp = input;
226             string[] strs = temp.Split(':');
227             if (strs.Length > 8)
228             {
229                 return false;
230             }
231             int count = MetarnetRegex.GetStringCount(input, "::");
232             if (count > 1)
233             {
234                 return false;
235             }
236             else if (count == 0)
237             {
238                 pattern = @"^([\da-f]{1,4}:){7}[\da-f]{1,4}$";
239 
240                 Regex regex = new Regex(pattern);
241                 return regex.IsMatch(input);
242             }
243             else
244             {
245                 pattern = @"^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$";
246                 Regex regex1 = new Regex(pattern);
247                 return regex1.IsMatch(input);
248             }
249 
250         }
251         /* *******************************************************************
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         * ******************************************************************/
259         /// <summary>
260         /// 判断字符串compare 在 input字符串中出现的次数
261         /// </summary>
262         /// <param name="input">源字符串</param>
263         /// <param name="compare">用于比较的字符串</param>
264         /// <returns>字符串compare 在 input字符串中出现的次数</returns>
265         private static int GetStringCount(string input, string compare)
266         {
267             int index = input.IndexOf(compare);
268             if (index != -1)
269             {
270                 return 1 + GetStringCount(input.Substring(index + compare.Length), compare);
271             }
272             else
273             {
274                 return 0;
275             }
276 
277         }
278     }
279 }

 


 


posted @ 2015-06-05 12:02  Dynamics365峰  阅读(2361)  评论(0编辑  收藏  举报