0

I am stuck on this regex problem.

A 16-digit credit card number, with the first digit being a 5 and the second digit being a 1, 2, 3, 4, or 5 (the rest of the digits can be anything).

so far I have ^4[1,5]\d{14} and I know I'm missing a lot of things but I dont know what I'm missing..

please help and thanks!

revo
  • 47,783
  • 14
  • 74
  • 117

2 Answers2

2

Look at the start of your regex:

^4[1,5]

That says that the number must start with 4 (not 5), and that the second character must be 1, a comma, or 5.

You want this instead (followed by the rest, of course):

^5[1-5]

Note the use of - rather than , to indicate a range of characters.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

The full regex you're looking for is the following

^5[1-5]\d{14}$

Demo

Your error lays in the fact that you used 1,5 as a range but this will just match 1 , or 5 as characters. To use a range, the - is needed between the enclosings

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89