Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Match the SSN patterns if both delimiter are same

need to match the ssn numbers only if both delimiters should match. But below code matches all.

((?:\d[-.\s]*?){9})

Input:

list of ssn are 222-33-4444, 333.77-8888 and 111 77.9998 and 111 22 3333 and 11-222222-9

Expected output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

222-33-4444
111 22 3333
11-222222-9

>Solution :

You can capture the first delimiter and then use a back-reference to assert that the second delimiter is the same character. Since the format can be variable in terms of delimiter placement, you also need to assert that there are 9 digits and 2 delimiters:

\b(?=[\d. -]{11}\b)\d{1,}([. -])\d{1,}\1\d{1,}\b

Demo on regex101

If the SSN may be adjacent to word characters, \b will not work (as there is no word boundary between a digit and a word character) and you will need to use negative lookarounds to assert the SSN is not preceded or followed by other digits:

(?<!\d)(?=[\d. -]{11}(?!\d))\d{1,}([. -])\d{1,}\1\d{1,}(?!\d)

Demo on regex101

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading