用于验证社会安全号码(SSN)的正则表达式,适用于一种格式。

huangapple go评论57阅读模式
英文:

regular expression to validate ssn for one format

问题

我已构建正则表达式以验证社会安全号码(SSN)的格式,以下是无效的格式 - 所有重复数字从0到9。例如:0000000001111111111等等,一直到999999999,或者包含连字符的格式如000-00-0000,以及其他一些无效格式,如1-9序列和9-1序列,有时带有连字符。

我想要将正则表达式添加到下面的正则表达式,以使111-22-3333111223333也被视为无效。

正则表达式: "^(?!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.

huangapple
  • 本文由 发表于 2023年4月7日 04:15:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定