forked from szgit/Helper.Core.Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegexExtendHelper.cs
169 lines (161 loc) · 4.94 KB
/
RegexExtendHelper.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Helper.Core.Library
{
public class RegexExtendHelper
{
#region 私有属性常量
#region 社会信息码常量
private static readonly Dictionary<string, int> SHXYM_CHAR_DICT = new Dictionary<string, int>()
{
{ "0", 0 },
{ "1", 1 },
{ "2", 2 },
{ "3", 3 },
{ "4", 4 },
{ "5", 5 },
{ "6", 6 },
{ "7", 7 },
{ "8", 8 },
{ "9", 9 },
{ "A", 10 },
{ "B", 11 },
{ "C", 12 },
{ "D", 13 },
{ "E", 14 },
{ "F", 15 },
{ "G", 16 },
{ "H", 17 },
{ "J", 18 },
{ "K", 19 },
{ "L", 20 },
{ "M", 21 },
{ "N", 22 },
{ "P", 23 },
{ "Q", 24 },
{ "R", 25 },
{ "T", 26 },
{ "U", 27 },
{ "W", 28 },
{ "X", 29 },
{ "Y", 30 },
};
private static readonly Dictionary<int, string> SHXYM_CHAR_DICT_V = new Dictionary<int, string>()
{
{ 0, "0" },
{ 1, "1" },
{ 2, "2" },
{ 3, "3" },
{ 4, "4" },
{ 5, "5" },
{ 6, "6" },
{ 7, "7" },
{ 8, "8" },
{ 9, "9" },
{ 10, "A" },
{ 11, "B" },
{ 12, "C" },
{ 13, "D" },
{ 14, "E" },
{ 15, "F" },
{ 16, "G" },
{ 17, "H" },
{ 18, "J" },
{ 19, "K" },
{ 20, "L" },
{ 21, "M" },
{ 22, "N" },
{ 23, "P" },
{ 24, "Q" },
{ 25, "R" },
{ 26, "T" },
{ 27, "U" },
{ 28, "W" },
{ 29, "X" },
{ 30, "Y" },
};
private static readonly string[] SHXYM_ONE_LIST = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "Y" };
private static readonly string[] SHXYM_TWO_LIST = new string[] { "1", "2", "3", "9" };
private static readonly int[] SHXYM_FACTOR_LIST = new int[] { 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28 };
#endregion
#region 身份证号常量
private static readonly Dictionary<int, int> IDENTITY_CARD_FACTOR_DICT = new Dictionary<int, int>()
{
{ 1, 7 },
{ 2, 9 },
{ 3, 10 },
{ 4, 5 },
{ 5, 8 },
{ 6, 4 },
{ 7, 2 },
{ 8, 1 },
{ 9, 6 },
{ 10, 3 },
{ 11, 7 },
{ 12, 9 },
{ 13, 10 },
{ 14, 5 },
{ 15, 8 },
{ 16, 4 },
{ 17, 2 },
};
private static readonly Dictionary<int, string> IDENTITY_CARD_CHAR_DICT = new Dictionary<int, string>()
{
{ 0, "1" },
{ 1, "0" },
{ 2, "X" },
{ 3, "9" },
{ 4, "8" },
{ 5, "7" },
{ 6, "6" },
{ 7, "5" },
{ 8, "4" },
{ 9, "3" },
{ 10, "2" },
};
#endregion
#endregion
#region 对外公开方法
/// <summary>
/// 是否社会信用码
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public static bool IsSHXYM(string code)
{
if (!Regex.IsMatch(code, @"^[0-9ABCDEFGHJKLMNPQRTUWXY]{18}$")) return false;
if (!SHXYM_ONE_LIST.Contains(code[0].ToString())) return false;
if (!SHXYM_TWO_LIST.Contains(code[1].ToString())) return false;
int total = 0;
for (int index = 0; index < code.Length - 1; index++)
{
total += SHXYM_CHAR_DICT[code[index].ToString()] * SHXYM_FACTOR_LIST[index];
}
int mod = 31 - total % 31;
if (SHXYM_CHAR_DICT_V.ContainsKey(mod) && SHXYM_CHAR_DICT_V[mod].ToString() != code[17].ToString()) return false;
return true;
}
/// <summary>
/// 是否身份证号
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public static bool IsIdentityCard(string code)
{
if (!Regex.IsMatch(code, @"^[0-9X]{18}$")) return false;
int total = 0;
for (int index = 1; index < code.Length; index++)
{
total += int.Parse(code[index - 1].ToString()) * IDENTITY_CARD_FACTOR_DICT[index];
}
int mod = total % 11;
if (code[17].ToString() != IDENTITY_CARD_CHAR_DICT[mod]) return false;
return true;
}
#endregion
}
}