-2

10 numeric digits, may be in the following formats: 123-4-567890, 1234-567890 or 1234567890

What is the regular expression for above digits?

Any help is appreciated.

Thanks.

IanCool
  • 1
  • 3

1 Answers1

0

Assuming you mean any digit, 0-9, should be found if (and only if) it meets the three formats you presented, one regex that would work is

(([0-9]{3}-[0-9]{1}-[0-9]{6})|([0-9]{4}-[0-9]{6})|([0-9]{10}))

The above breaks down to three separate patterns, one for each case you presented, separated by regex's equivalent of "or", the | character. Each of the statements above contains [0-9], a character class which will match any digit. Following each character class is a {n} statement, which means "repeat the previous item n times".

Disclaimer, there is probably a cleverer way to do this with a shorter pattern, but my regex-foo isn't quite that advanced yet

romeara
  • 1,426
  • 1
  • 17
  • 26