-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRegexHelper.cs
More file actions
393 lines (381 loc) · 16.4 KB
/
RegexHelper.cs
File metadata and controls
393 lines (381 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* 作用:常用正则表达式验证。
* */
using System.Text.RegularExpressions;
namespace Helper.Core.Library
{
#region 逻辑处理辅助枚举
public enum RegexTypeEnum : int
{
/// <summary>
/// 成功
/// </summary>
Success = 0,
/// <summary>
/// 范围不正确
/// </summary>
Range = 1,
/// <summary>
/// 长度
/// </summary>
Length = 2,
/// <summary>
/// 失败
/// </summary>
Failed = 3
}
#endregion
public class RegexHelper
{
#region 对外公开方法
/// <summary>
/// 匹配,不区分大小写
/// </summary>
/// <param name="input"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public static bool IsMatch(string input, string pattern)
{
return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
}
#region RegexTypeEnum
/// <summary>
/// 验证整数
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="min">最小值/长度</param>
/// <param name="max">最大值/长度</param>
/// <param name="isRange">是否范围,false 按长度检查,true 按数值范围检查</param>
/// <returns></returns>
public static RegexTypeEnum Int(string input, int min = 0, int max = 0, bool isRange = false)
{
bool matchStatus = IsMatch(input, @"^-?[0-9]+$");
if (!matchStatus) return RegexTypeEnum.Failed;
if (max > 0 && max >= min)
{
if (!isRange)
{
matchStatus = IsMatch(input, @"^-?[0-9]{m,n}$".Replace("m", min.ToString()).Replace("n", max.ToString()));
if (!matchStatus) return RegexTypeEnum.Length;
}
else
{
if (int.Parse(input) < min || int.Parse(input) > max) return RegexTypeEnum.Range;
}
}
return RegexTypeEnum.Success;
}
/// <summary>
/// 验证浮点数
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="dotMin">小数点最小长度</param>
/// <param name="dotMax">小数点最大长度</param>
/// <param name="min">最小值/长度</param>
/// <param name="max">最大值/长度</param>
/// <param name="isRange">是否范围,false 按长度检查,true 按数值范围检查</param>
/// <returns></returns>
public static RegexTypeEnum Float(string input, int dotMin = 0, int dotMax = 0, float min = 0f, float max = 0f, bool isRange = false)
{
string pattern = @"^-?[0-9]+([.][0-9]+)?$";
if (dotMax > 0 && dotMin > 0 && dotMin <= dotMax) pattern = @"^-?[0-9]+[.][0-9]{m,n}$".Replace("m", dotMin.ToString()).Replace("n", dotMax.ToString());
bool matchStatus = IsMatch(input, pattern);
if (!matchStatus) return RegexTypeEnum.Failed;
if (max > 0f && max >= min)
{
if (!isRange)
{
pattern = @"^-?[0-9]{m,n}([.][0-9]+)?$".Replace("m", min.ToString("f0")).Replace("n", max.ToString("f0"));
if (dotMax > 0 && dotMin > 0 && dotMin <= dotMax) pattern = @"^-?[0-9]{m,n}[.][0-9]{a,b}$".Replace("m", min.ToString("f0")).Replace("n", max.ToString("f0")).Replace("a", dotMin.ToString()).Replace("b", dotMax.ToString());
matchStatus = IsMatch(input, pattern);
if (!matchStatus) return RegexTypeEnum.Length;
}
else
{
if (float.Parse(input) < min || float.Parse(input) > max) return RegexTypeEnum.Range;
}
}
return RegexTypeEnum.Success;
}
/// <summary>
/// 验证字母
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="min">最小长度</param>
/// <param name="max">最大长度</param>
/// <returns></returns>
public static RegexTypeEnum Alphabet(string input, int min = 0, int max = 0)
{
bool matchStatus = IsMatch(input, @"^[a-zA-Z]+$");
if (!matchStatus) return RegexTypeEnum.Failed;
if (max > 0 && max >= min)
{
matchStatus = IsMatch(input, @"^[a-zA-Z]{m,n}$".Replace("m", min.ToString()).Replace("n", max.ToString()));
if (!matchStatus) return RegexTypeEnum.Length;
}
return RegexTypeEnum.Success;
}
/// <summary>
/// 验证字母/特殊字符
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="spec">特殊字符</param>
/// <param name="min">最小长度</param>
/// <param name="max">最大长度</param>
/// <returns></returns>
public static RegexTypeEnum Alphabet(string input, string spec, int min = 0, int max = 0)
{
bool matchStatus = IsMatch(input, @"^[a-zA-Z*]+$".Replace("*", spec));
if (!matchStatus) return RegexTypeEnum.Failed;
if (max > 0 && max >= min)
{
matchStatus = IsMatch(input, @"^[a-zA-Z*]{m,n}$".Replace("*", spec).Replace("m", min.ToString()).Replace("n", max.ToString()));
if (!matchStatus) return RegexTypeEnum.Length;
}
return RegexTypeEnum.Success;
}
/// <summary>
/// 验证字母数字
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="min">最小长度</param>
/// <param name="max">最大长度</param>
/// <returns></returns>
public static RegexTypeEnum AlphabetDigit(string input, int min = 0, int max = 0)
{
bool matchStatus = IsMatch(input, @"^[a-zA-Z0-9]+$");
if (!matchStatus) return RegexTypeEnum.Failed;
if (max > 0 && max >= min)
{
matchStatus = IsMatch(input, @"^[a-zA-Z0-9]{m,n}$".Replace("m", min.ToString()).Replace("n", max.ToString()));
if (!matchStatus) return RegexTypeEnum.Length;
}
return RegexTypeEnum.Success;
}
/// <summary>
/// 验证字母数字/特殊字符
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="spec">特殊字符</param>
/// <param name="min">最小长度</param>
/// <param name="max">最大长度</param>
/// <returns></returns>
public static RegexTypeEnum AlphabetDigit(string input, string spec, int min = 0, int max = 0)
{
bool matchStatus = IsMatch(input, @"^[a-zA-Z0-9*]+$".Replace("*", spec));
if (!matchStatus) return RegexTypeEnum.Failed;
if (max > 0 && max >= min)
{
matchStatus = IsMatch(input, @"^[a-zA-Z0-9*]{m,n}$".Replace("*", spec).Replace("m", min.ToString()).Replace("n", max.ToString()));
if (!matchStatus) return RegexTypeEnum.Length;
}
return RegexTypeEnum.Success;
}
/// <summary>
/// 验证汉字
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="min">最小长度</param>
/// <param name="max">最大长度</param>
/// <returns></returns>
public static RegexTypeEnum Chinese(string input, int min = 0, int max = 0)
{
bool matchStatus = IsMatch(input, @"^[\u4e00-\u9fa5]+$");
if (!matchStatus) return RegexTypeEnum.Failed;
if (max > 0 && max >= min)
{
matchStatus = IsMatch(input, @"^[\u4e00-\u9fa5]{m,n}$".Replace("m", min.ToString()).Replace("n", max.ToString()));
if (!matchStatus) return RegexTypeEnum.Length;
}
return RegexTypeEnum.Success;
}
/// <summary>
/// 验证汉字/特殊字符
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="spec">特殊字符</param>
/// <param name="min">最小长度</param>
/// <param name="max">最大长度</param>
/// <returns></returns>
public static RegexTypeEnum Chinese(string input, string spec, int min = 0, int max = 0)
{
bool matchStatus = IsMatch(input, @"^[\u4e00-\u9fa5*]+$".Replace("*", spec));
if (!matchStatus) return RegexTypeEnum.Failed;
if (max > 0 && max >= min)
{
matchStatus = IsMatch(input, @"^[\u4e00-\u9fa5*]{m,n}$".Replace("*", spec).Replace("m", min.ToString()).Replace("n", max.ToString()));
if (!matchStatus) return RegexTypeEnum.Length;
}
return RegexTypeEnum.Success;
}
/// <summary>
/// 验证双字节
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="min">最小长度</param>
/// <param name="max">最大长度</param>
/// <returns></returns>
public static RegexTypeEnum DoubleByte(string input, int min = 0, int max = 0)
{
bool matchStatus = IsMatch(input, @"^[^\x00-\xff]+$");
if (!matchStatus) return RegexTypeEnum.Failed;
if (max > 0 && max >= min)
{
matchStatus = IsMatch(input, @"^[^\x00-\xff]{m,n}$".Replace("m", min.ToString()).Replace("n", max.ToString()));
if (!matchStatus) return RegexTypeEnum.Length;
}
return RegexTypeEnum.Success;
}
/// <summary>
/// 验证中文英文数字
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="min">最小长度</param>
/// <param name="max">最大长度</param>
/// <returns></returns>
public static RegexTypeEnum Word(string input, int min = 0, int max = 0)
{
bool matchStatus = IsMatch(input, @"^[\u4e00-\u9fa5A-Za-z0-9]+$");
if (!matchStatus) return RegexTypeEnum.Failed;
if (max > 0 && max >= min)
{
matchStatus = IsMatch(input, @"^[\u4e00-\u9fa5A-Za-z0-9]{m,n}$".Replace("m", min.ToString()).Replace("n", max.ToString()));
if (!matchStatus) return RegexTypeEnum.Length;
}
return RegexTypeEnum.Success;
}
/// <summary>
/// 验证中文英文数字/特殊字符
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="spec">特殊字符</param>
/// <param name="min">最小长度</param>
/// <param name="max">最大长度</param>
/// <returns></returns>
public static RegexTypeEnum Word(string input, string spec, int min = 0, int max = 0)
{
bool matchStatus = IsMatch(input, @"^[\u4e00-\u9fa5A-Za-z0-9*]+$".Replace("*", spec));
if (!matchStatus) return RegexTypeEnum.Failed;
if (max > 0 && max >= min)
{
matchStatus = IsMatch(input, @"^[\u4e00-\u9fa5A-Za-z0-9*]{m,n}$".Replace("*", spec).Replace("m", min.ToString()).Replace("n", max.ToString()));
if (!matchStatus) return RegexTypeEnum.Length;
}
return RegexTypeEnum.Success;
}
#endregion
#region Boolean
/// <summary>
/// 验证是否整数
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <returns></returns>
public static bool IsInt(string input)
{
return Int(input) == RegexTypeEnum.Success;
}
/// <summary>
/// 验证是否符点数
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <returns></returns>
public static bool IsFloat(string input)
{
return Float(input) == RegexTypeEnum.Success;
}
/// <summary>
/// 验证是否字母/特殊字符
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="spec">特殊字符</param>
/// <returns></returns>
public static bool IsAlphabet(string input, string spec = null)
{
if (string.IsNullOrEmpty(spec)) return Alphabet(input) == RegexTypeEnum.Success;
return Alphabet(input, spec) == RegexTypeEnum.Success;
}
/// <summary>
/// 验证是否字母数字/特殊字符
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="spec">特殊字符</param>
/// <returns></returns>
public static bool IsAlphabetDigit(string input, string spec = null)
{
if (string.IsNullOrEmpty(spec)) return AlphabetDigit(input) == RegexTypeEnum.Success;
return AlphabetDigit(input, spec) == RegexTypeEnum.Success;
}
/// <summary>
/// 验证是否汉字/特殊字符
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="spec">特殊字符</param>
/// <returns></returns>
public static bool IsChinese(string input, string spec = null)
{
if (string.IsNullOrEmpty(spec)) return Chinese(input) == RegexTypeEnum.Success;
return Chinese(input, spec) == RegexTypeEnum.Success;
}
/// <summary>
/// 验证是否双字节
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <returns></returns>
public static bool IsDoubleByte(string input)
{
return DoubleByte(input) == RegexTypeEnum.Success;
}
/// <summary>
/// 验证是否中文英文数字/特殊字符
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="spec">特殊字符</param>
/// <returns></returns>
public static bool IsWord(string input, string spec = null)
{
if (string.IsNullOrEmpty(spec)) return Word(input) == RegexTypeEnum.Success;
return Word(input, spec) == RegexTypeEnum.Success;
}
/// <summary>
/// 验证是否邮件
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <returns></returns>
public static bool IsEmail(string input)
{
return IsMatch(input, @"^[a-z0-9]+([._-]*[a-z0-9]+)*@[a-z0-9]+([.][a-z0-9]+)*$");
}
/// <summary>
/// 验证是否短日期 yyyy-MM-dd
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <returns></returns>
public static bool IsDate(string input)
{
string pattern = @"^(((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((04|08|12|16|[2468][048]|[3579][26])00))-0?2-29))$";
return IsMatch(input, pattern);
}
/// <summary>
/// 验证是否长日期 yyyy-MM-dd hh:mm:ss
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <returns></returns>
public static bool IsFullDate(string input)
{
string pattern = @"^(((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((04|08|12|16|[2468][048]|[3579][26])00))-0?2-29)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$";
return IsMatch(input, pattern);
}
/// <summary>
/// 验证是否是脚本
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsScript(string input)
{
string pattern = @"[<>*]+";
return IsMatch(input, pattern);
}
#endregion
#endregion
}
}