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:


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 * */*;
    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, 2016 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 May 24, 2016 at 6:23

2 Answers 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]?"                                                                                        

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

This regex will match valid Visa, MC and Amex:


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:




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. May 24, 2016 at 9:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .