英文:
regular expression to validate ssn for one format
问题
我已构建正则表达式以验证社会安全号码(SSN)的格式,以下是无效的格式 - 所有重复数字从0到9。例如:000000000,1111111111等等,一直到999999999,或者包含连字符的格式如000-00-0000,以及其他一些无效格式,如1-9序列和9-1序列,有时带有连字符。
我想要将正则表达式添加到下面的正则表达式,以使111-22-3333和111223333也被视为无效。
正则表达式: "^(?!123([ -]?)45([ -]?)6789)(?!\b(\d)\3+\b)(?!000|666|9)[0-9]{3}([ -]?)(?!00)[0-9]{2}\4(?!0000)[0-9]{4}$"
正则表达式对我需要的所有格式都有效。我只需要为111-22-3333和没有连字符的情况添加正则表达式。
英文:
I have constructed regular expression to validate ssn for the following
invalid formats - all repeated number 0 to 9.
For example : 000000000, 1111111111 and so on till 999999999 or with hyphens 000-00-0000. with some other invalid formats 1-9 sequence and 9-1 sequence with and without hyphens.
I would like to include reg-ex for 111-22-3333 and 111223333 as invalid to the below reg-ex.
Any quick suggestions please.
reg-ex: "^(?!123([ -]?)45([ -]?)6789)(?!\b(\d)\3+\b)(?!000|666|9)[0-9]{3}([ -]?)(?!00)[0-9]{2}\4(?!0000)[0-9]{4}$"
Reg-ex is working for all formats I needed. I would need to add only for 111-22-3333 and without hyphens.
答案1
得分: 1
Since your regex already forbids same digit first block, 111-22-3333
and 111 22 3333
are of the table.
To exclude 111223333
you can simply add one more negative lookahead in the beginning:
^(?!111223333)(?!123([ -]?)45([ -]?)6789)(?!\b(\d)+\b)(?!000|666|9)[0-9]{3}([ -]?)(?!00)[0-9]{2}(?!0000)[0-9]{4}$
Demo here.
英文:
Since your regex already forbids same digit first block, 111-22-3333
and 111 22 3333
are of the table.
To exclude 111223333
you can simply add one more negative lookahead in the beginning:
^(?!111223333)(?!123([ -]?)45([ -]?)6789)(?!\b(\d)+\b)(?!000|666|9)[0-9]{3}([ -]?)(?!00)[0-9]{2}(?!0000)[0-9]{4}$
Demo here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论