-1

I need your help with this one. I have to form a regex that accepts only 8 digits (no letters) with no or less than 3 dashes.

It should accept these examples:

12345678
1234 5678
1234-5678
123-45-678
12-345-678

It should not accept these examples:

1234 (less than 8 digits)
123456789 (more than 8 digits)
-12345678-
!@@#$%
12ABCBDEF (with letters)
12-34-56-78 (more than 3 dashes)

Any help is appreciated.

Tsung-Ting Kuo
  • 1,171
  • 6
  • 16
  • 21
FSammyson
  • 11
  • 5

1 Answers1

1
^(?=(?:\D*\d){8}$)\d+(?:(?:-\d+){0,2}|(?:\s+\d+){0,1})$

You can try this.See demo.

https://regex101.com/r/hE4jH0/20

vks
  • 67,027
  • 10
  • 91
  • 124