(DISCLAIMER: This is a homework exercise)
I have a file with several registers, each of them has a credit card number. I have to retrieve those registers that belong to an AMEX card, which begin with 34, 35 or 36 and have 15 digits overal like this:
XXXX-XXXX-XXXX-XXX
Separated by hyphens every 4 digits.
I tried using grep over that file to detect those numbers, but to no avail. This is the original command I used:
grep -E "[34-36][0-9]{2}[\-]([0-9]{4}[\-]){2}[0-9]{3}" MYFILE.txt
Which I assumed it would work, because I think the regular expression means:
"A number between 34 and 36, followed by 2 numbers from 0 to 9, followed by a hyphen, followed by 4 numbers from 0 to 9 followed by a hyphen (this twice) followed by 3 numbers from 0 to 9".
But this command retrieved nothing. For the record, I have this register:
3435-9999-8765-333
in the file, so I should at least retrieve that.
What's funny is that if I change the regular expression to just [34-36][0-9]{2}
, I get a match (obviously), and if I add the hyphen ([34-36][0-9]{2}[\-]
) I still get a match, but if I add anything after it (like [34-36][0-9]{2}[\-][0-9]
, [34-36][0-9]{2}[\-]a
while changing the record to have an "a" after the first two digits) I stop retrieving anything.
What am I doing wrong? Is there a problem with the hyphen in the regular expression? Which would be the right one?
Thanks.