1

I have used the following regular expression to detect the valid card numbers.

[2-6][0-9]{3}([ -]?)[0-9]{4}([ -]?)[0-9]{4}([ -]?)[0-9]{3,4}([ -]?)[0-9]{0,3}[^a-zA-Z]

But it does not detect the followings:

374355011240344
373116614411000

But if I use the same in the site: http://www.regexr.com/, all those are detected.

Is there any compatibility issue with this?

What I want is to detect any pattern:

  1. starting from 2,3,4,5,6
  2. could be delimitted by " " or "-" at 4-digit intervals (2345-3456-2345-2345 / 4567 6745 5645 4534 345)
  3. length could be 13 to 19

Here is the command that i have used.

for i in * */*;
do
    grep -ocE "[2-6][0-9]{3}([ -]?)[0-9]{4}([ -]?)[0-9]{4}([ -]?)[0-9]{3,4}([ -]?)[0-9]{0,3}[^a-zA-Z]" "$i";
done    `
  • 3
    How are you using the pattern within a script? Please provide a sample of your invocation and how you input the data to be tested. This looks more like a sed regex than bash pattern. – Anthon May 24 '16 at 5:52
  • regexr.com uses javascript regular expressions they different to regular expressions you are likely to come across with anything from the UNIX ecosystem of programs – the_velour_fog May 24 '16 at 6:23
2

There are variances and limitations in how regexs get evaluated based on the utility being provided the expression, the arguments used when calling the command, the shell the command is called in, among other issues.

With that said, the last section of the regex is what is blocking the match when I use it with grep; it is not listed as optional so it is exceeding the character count (looking for not letters).

[{0} 01:49:15] $ echo "374355011240344" | grep -Eo "[2-6][0-9]{3}([ -]?)[0-9]{4}([ -]?)[0-9]{4}([ -]?)[0-9]{3,4}([ -]?)[0-9]{0,3}[^a-zA-Z]"                                                                                                                                                                                                                    
[{1} 01:49:33] $ echo "374355011240344" | grep -Eo "[2-6][0-9]{3}([ -]?)[0-9]{4}([ -]?)[0-9]{4}([ -]?)[0-9]{3,4}([ -]?)[0-9]{0,3}[^a-zA-Z]?"                                                                                        
374355011240344

The following site wrote the book on credit card regexes: credit card regex

This regex will match valid Visa, MC and Amex:

"^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})$"

The link I provided shows how to 'grow' that regex to cover all major CCs. Their general approach (which I agree with, for what its worth), is to strip out extraneous characters prior to pattern matching - removing spaces, dashes, etc. It might be worth looking into.

A couple more sites:

https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9781449327453/ch04s20.html

http://reluctanthacker.rollett.org/creating-regex-finding-credit-card-numbers-grep

0

This would better done using the luhn algorithm a discussion of how to implement it in bash can be found here https://codereview.stackexchange.com/questions/95211/validating-a-credit-card-number-using-luhns-algorithm

  • Thanks. But satisfying the loon is not enough for a card number to be valid. – user3148655 May 24 '16 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.