heyuntao
2023-05-04 a83e860e3b9fd68c9f2a478b885db7fa451bb1a6
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
using CommonUtil.Top;
using System;
using System.Linq;
using System.Text.RegularExpressions;
 
namespace CommonUtil
{
    /// <summary>
    /// 扩展方法工具类
    /// </summary>
    public static partial class ExtendUtil
    {
        private static readonly Regex REG_CIDR = new Regex("^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})/(\\d{1,2})$");
 
        /// <summary>
        /// 是否为null
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>bool</returns>
        public static bool IsNull(this object data)
        {
            return data == null;
        }
 
        /// <summary>
        /// 是否为空或仅有空白字符组成
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>bool</returns>
        public static bool IsNullOrEmpty(this string data)
        {
            return string.IsNullOrEmpty(data);
        }
 
        /// <summary>
        /// 是否为空或仅有空白字符组成
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>bool</returns>
        public static bool IsNullOrWhiteSpace(this string data)
        {
            return string.IsNullOrWhiteSpace(data);
        }
 
        /// <summary>
        /// 是否为空,null也算作空
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>bool</returns>
        public static bool IsNullOrEmpty(this Guid? data)
        {
            if (!data.HasValue)
            {
                return true;
            }
            return IsNullOrEmpty(data.Value);
        }
 
        /// <summary>
        /// 是否为空
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>bool</returns>
        public static bool IsNullOrEmpty(this Guid data)
        {
            if (data == Guid.Empty)
            {
                return true;
            }
 
            return false;
        }
 
      
        //public static bool IsNumber(this string str)
        //{
        //    string pattern = @"^[+-]?\d*[.]?\d*$";
 
        //    return Regex.IsMatch(str, pattern);
        //}
 
        /// <summary>
        /// 判断是否是正确的手机号码格式
        /// </summary>
        /// <param name="txtPhone">手机号码</param>
        /// <returns>bool</returns>
        public static bool IsPhone(this string txtPhone)
        {
            string rule = @"^(1(([3457968][0-9])|(47)|[8][01236789]))\d{8}$";
            return Regex.IsMatch(txtPhone, rule);
        }
 
        /// <summary>
        /// 判断是否是正确的邮箱格式
        /// </summary>
        /// <param name="txtEmail">邮箱号码</param>
        /// <returns>bool</returns>
        public static bool IsEmail(this string txtEmail)
        {
            Regex rule = new Regex(@"^\s*([A-Za-z0-9_-]+(\.\w+)*@(\w+\.)+\w{2,5})\s*$");
            return rule.IsMatch(txtEmail);
        }
 
        /// <summary>
        /// 验证是否为url
        /// </summary>
        /// <param name="str">数据</param>
        /// <returns>bool</returns>
        public static bool IsUrl(this string str)
        {
            return Regex.IsMatch(str, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&$%\$#\=~])*$");
        }
 
        /// <summary>
        /// 验证是否为IP地址
        /// </summary>
        /// <param name="str">数据</param>
        /// <returns>bool</returns>
        public static bool IsIP(this string str)
        {
            return Regex.IsMatch(str, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
 
        /// <summary>
        /// 验证ip是否在范围
        /// </summary>
        /// <param name="ipAddr"></param>
        /// <param name="cidrAddr"></param>
        /// <returns></returns>
        public static bool IsIpInRange(string ipAddr, string cidrAddr)
        {
            Match match = REG_CIDR.Match(cidrAddr);
            if (!match.Success)
            {
                throw new TopException("Invalid CIDR address: " + cidrAddr);
            }
 
            int[] minIpParts = new int[4];
            int[] maxIpParts = new int[4];
            string[] ipParts = match.Groups[1].Value.Split(new string[1] { "." }, StringSplitOptions.None);
            int intMask = int.Parse(match.Groups[2].Value);
 
            for (int i = 0; i < ipParts.Length; i++)
            {
                int ipPart = int.Parse(ipParts[i]);
                if (intMask > 8)
                {
                    minIpParts[i] = ipPart;
                    maxIpParts[i] = ipPart;
                    intMask -= 8;
                }
                else if (intMask > 0)
                {
                    minIpParts[i] = ipPart >> intMask;
                    maxIpParts[i] = ipPart | (0xFF >> intMask);
                    intMask = 0;
                }
                else
                {
                    minIpParts[i] = 1;
                    maxIpParts[i] = 0xFF - 1;
                }
            }
 
            string[] realIpParts = ipAddr.Split(new string[1] { "." }, StringSplitOptions.None);
            for (int i = 0; i < realIpParts.Length; i++)
            {
                int realIp = int.Parse(realIpParts[i]);
                if (realIp < minIpParts[i] || realIp > maxIpParts[i])
                {
                    return false;
                }
            }
            return true;
        }
 
        /// <summary>
        /// 值在的范围?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <param name="begin">大于等于begin</param>
        /// <param name="end">小于等于end</param>
        /// <returns>bool</returns>
        public static bool IsInRange(this int thisValue, int begin, int end)
        {
            return thisValue >= begin && thisValue <= end;
        }
 
        /// <summary>
        /// 值在的范围?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <param name="begin">大于等于begin</param>
        /// <param name="end">小于等于end</param>
        /// <returns>bool</returns>
        public static bool IsInRange(this DateTime thisValue, DateTime begin, DateTime end)
        {
            return thisValue >= begin && thisValue <= end;
        }
 
        /// <summary>
        /// 长度在范围?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <param name="minLength">大于等于最小长度</param>
        /// <param name="maxLength">小于等于最大长度</param>
        /// <returns></returns>
        public static bool IsInRange(this string thisValue, long minLength, long maxLength)
        {
            return thisValue.Length >= minLength && thisValue.Length <= maxLength;
        }
 
        /// <summary>
        /// 在里面吗?
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="thisValue">数据</param>
        /// <param name="inValues">匹配范围</param>
        /// <returns>bool</returns>
        public static bool IsContains<T>(this T thisValue, params T[] inValues) where T : struct
        {
            return inValues.Contains(thisValue);
        }
 
        /// <summary>
        /// 在里面吗?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <param name="inValues">匹配范围</param>
        /// <returns>bool</returns>
        public static bool IsContains(this string thisValue, params string[] inValues)
        {
            return inValues.Any(it => thisValue.Contains(it));
        }
 
        /// <summary>
        /// 是否包含空白
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <returns></returns>
        public static bool IsContainsEmpty(this string thisValue)
        {
            if (thisValue.IsNullOrWhiteSpace()) return true;
            return Regex.IsMatch(thisValue, @"\s+");
        }
 
        /// <summary>
        /// 是零?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <returns>bool</returns>
        public static bool IsZero(this int thisValue)
        {
            return thisValue == 0;
        }
 
        /// <summary>
        /// 是零?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <returns>bool</returns>
        public static bool IsZero(this decimal thisValue)
        {
            return thisValue == 0;
        }
 
        /// <summary>
        /// 是零?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <returns>bool</returns>
        public static bool IsZero(this double thisValue)
        {
            return thisValue == 0;
        }
 
        /// <summary>
        /// 是零?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <returns>bool</returns>
        public static bool IsZero(this float thisValue)
        {
            return thisValue == 0;
        }
 
        /// <summary>
        /// 是INT?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <returns>bool</returns>
        public static bool IsInt(this string thisValue)
        {
            return Regex.IsMatch(thisValue, @"^\d+$");
        }
 
        /// <summary>
        /// 不是INT?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <returns>bool</returns>
        public static bool IsNoInt(this string thisValue)
        {
            return !Regex.IsMatch(thisValue, @"^\d+$");
        }
 
        /// <summary>
        /// 是时间?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <returns>bool</returns>
        public static bool IsDateTime(this string thisValue)
        {
            DateTime outValue = DateTime.MinValue;
            return DateTime.TryParse(thisValue, out outValue);
        }
 
        /// <summary>
        /// 是身份证?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <returns>bool</returns>
        public static bool IsIDcard(this string thisValue)
        {
            return Regex.IsMatch(thisValue, @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
        }
 
        /// <summary>
        /// 是传真?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <returns>bool</returns>
        public static bool IsFax(this string thisValue)
        {
            return Regex.IsMatch(thisValue, @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
        }
 
        /// <summary>
        /// 是适合正则匹配?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <param name="pattern">正则表达式</param>
        /// <returns>bool</returns>
        public static bool IsMatch(this string thisValue, string pattern)
        {
            Regex reg = new Regex(pattern);
            return reg.IsMatch(thisValue);
        }
 
        /// <summary>
        /// 是适合正则匹配?
        /// </summary>
        /// <param name="thisValue">数据</param>
        /// <param name="reg">正则表达式</param>
        /// <returns>bool</returns>
        public static bool IsMatch(this string thisValue, Regex reg)
        {
            return reg.IsMatch(thisValue);
        }
 
 
    }
}