项目中用到了身份证校验,以前用的经验算法出现了问题,因此从androidUtil上直接找到了相关检验的方案
/**
* Regex of id card number which length is 15.
*/
public static final String REGEX_ID_CARD15 = “1\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}
"
;
/
∗
∗
∗
R
e
g
e
x
o
f
i
d
c
a
r
d
n
u
m
b
e
r
w
h
i
c
h
l
e
n
g
t
h
i
s
18.
∗
/
p
u
b
l
i
c
s
t
a
t
i
c
f
i
n
a
l
S
t
r
i
n
g
R
E
G
E
X
I
D
C
A
R
D
18
=
"
[
1
−
9
]
d
5
[
1
−
9
]
d
3
(
(
0
d
)
∣
(
1
[
0
−
2
]
)
)
(
(
[
0
∣
1
∣
2
]
d
)
∣
3
[
0
−
1
]
)
d
3
(
[
0
−
9
X
x
]
)
"; /** * Regex of id card number which length is 18. */ public static final String REGEX_ID_CARD18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])
";/∗∗∗Regexofidcardnumberwhichlengthis18.∗/publicstaticfinalStringREGEXIDCARD18="[1−9]d5[1−9]d3((0d)∣(1[0−2]))(([0∣1∣2]d)∣3[0−1])d3([0−9Xx])”;
pu
blic static boolean isRightIdCard( final CharSequence input){
if (input.length()>15){
return isMatch(REGEX_ID_CARD18,input);
}else {
return isMatch(REGEX_ID_CARD15,input);
}
}
/**
* Return whether input matches the regex.
*
* @param regex The regex.
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isMatch(final String regex, final CharSequence input) {
return input != null && input.length() > 0 && Pattern.matches(regex, input);
}
/**
* Return the list of input matches the regex.
*
* @param regex The regex.
* @param input The input.
* @return the list of input matches the regex
*/
public static List<String> getMatches(final String regex, final CharSequence input) {
if (input == null) return Collections.emptyList();
List<String> matches = new ArrayList<>();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
matches.add(matcher.group());
}
return matches;
}
1-9 ↩︎