怎么写一条正则去匹配15位、18位、还有最后一个字符为字母的身份证号

大佬们,我想用一条正则表达式在一堆字符串里面去匹配匹配15位、18位、还有最后一个字符为字母的身份证号。那一堆字符串如下:

|_ Potentially risky methods: TRACE
|_http-server-header: /2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.4.45
|_http-title: 403 Forbidden
88/tcp filtered kerberos-sec
135/tcp filtered msrpc
139/tcp filtered netbios-ssn
389/tcp filtered ldap
440923195906209416445/tcp filtered microsoft-ds
Device type: general purpose|media device|router|specialized|WAP440923195906207971
Running (JUST GUESSING): Microsoft Windows 2003|XP|2000 (92%), Microsoft embedde
d (88%), Linksys embedded (87%), AVtech embedded (85%), Apple embedded (85%)
OS CPE: cpe:/o:microsoft:windows_server_2003::sp1 cpe:/o:microsoft:windows_serve
r_2003::sp2 cpe:/o:micr440923195906206717osoft:windows_xp::sp3 cpe::airport_extreme cpe:/o
:microsoft:windows_2000::sp4:server
Aggressive OS guesses: Microsoft Windows Server 2003 SP1 or SP2 (92%), Microsoft
Window 44092319590620907xs XP SP3 or
irmware 3.1) (88%), Linksys BEFSR41 EtherFast router (87%), Microsoft Windows Se
rver 2003 SP2 (87%), AVtech Room Alert 26W environmental monitor (85%), Microsof
t Windows XP SP2 or SP3 (85%), Apple AirPort Extreme WAP (85%), Microsoft Window
s 2000 Server SP4 or Windows XP Professional SP3 (85%), Microsoft Windows 2000 S
P4 (85%)
No exact OS matches for host (test conditions non-ideal).
Network Distance: 15 hops
Service Info: OS: Windows; CPE: cpe:/o:microsoft:windows

已知有一条可以匹配18位的正则:
[1-9]d{5}(?:19|20)dd(?:0[1-9]|1[012])(?:0[1-9]|[12]d|3[01])d{4}
会匹配到一下身份证:
440923195906209416
440923195906207971
440923195906206717
但是不会匹配到
44092319590620907x

求各位大佬指点一下怎么改一下这条正则表达式!谢谢!

PS:以上身份证号都是随机生成的身份证号,并非真实身份证号,请熟知。

PS:我想用python去实现···

阅读 7k
6 个回答

18位

(?:1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5])\d{4}(?:1[89]|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}(?:\d|[xX])

15位

(?:1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5])\d{4}\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}
/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])(\d{3}[Xx])$)$/

身份证最后一位应该只有X吧

正常的身份证匹配为:/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/

/^(^[1-9]d{7}((0d)|(1[0-2]))(([0|1|2]d)|3[0-1])d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/

介绍

xxxxxx yyyy MM dd 375 0 十八位

xxxxxx yy MM dd 75 0 十五位

地区: [1-9]d{5}

年的前两位: (18|19|([23]d)) 1800-2399

年的后两位: d{2}

月份: ((0[1-9])|(10|11|12))

天数: ((0-2)|10|20|30|31) 闰年不能禁止29+

三位顺序码: d{3}

两位顺序码: d{2}

校验码: [0-9Xx]

正则表达式

十八位: ^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$

十五位: ^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$

总:

( ^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)

你可以试试这个,我在我本地试了是好用的

<input type="text" id="inputVal" >
<div id="test">测试</div>
let test= document.getElementById("test");
test.addEventListener("click", function () {
   let input = document.getElementById("inputVal").value;
   let reg = /(^\d{15}$)|(^\d{17}([0-9]|X)$)/;
   if (!reg.test(input)) {
     alert("你的身份证号码格式不对!");
   } else {
     alert("OK!");
   }
});    

根据身份证编码格式可以试试这个:
18位:

(?<n1>[1,6][1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|71|8[1-2])\d{4}(?<n1>19|20)\d{2}(?<n3>0[1,3,5,7,8](0[1-9]|[1,2]\d|3[0,1])|02(0[1-9]|[1-2]\d)|0[4,6,9](0[1-9]|[1,2]\d|30)|1[0,2](0[1-9]|[1,2]\d|3[0,1])|11(0[1-9]|[1,2]\d|30))\d{3}(?<n4>\d|[x,X])

15位:

(?<n1>[1,6][1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|71|8[1-2])\d{6}(?<n3>0[1,3,5,7,8](0[1-9]|[1,2]\d|3[0,1])|02(0[1-9]|[1-2]\d)|0[4,6,9](0[1-9]|[1,2]\d|30)|1[0,2](0[1-9]|[1,2]\d|3[0,1])|11(0[1-9]|[1,2]\d|30))\d{3}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题