Regular expression to detect the credit card flag when there are specific characters in the number

Asked

Viewed 1,086 times

4

I need to create a regular expression (Regex) that can identify the card number even when there are specific characters between the numbers,

Example:
Current regex: ((\d{16}|\d{4}(.\d{4}){3})|(\d{4}(\d{4}){3}))
Allowing me to read the number of cards when placed as follows:

4012001037141112
4012 0010 3714 1112
4012/0010/3714/1112
4012.0010.3714.1112
4012*0010*3714*1112

But I need to create a regex that identifies the card number, without looking at which field the specific character is typed, some examples of how the card number could appear and which regex above does not identify:

4/0/1/2/0/0/1/0/3/7/1/4/1/1/1/2
4.0.1.2.0.0.1.0.3.7.1.4.1.1.1.2
4.0.12001037141112
401200103714/1112
401*200103714111*2

The regex I have today works only when the character is typed after the 4 house, I want it to have the same action regardless of where the specific character is.

Could someone help me? These settings will use on DLP, me I don’t have access to the programming part only to a field where I put the regular expression.

  • Sanitize the string to only have numbers and validate normally. What you’re aiming for has no logical sense.

  • 1

    In what language do you want to do this? Take a look here: http://answall.com/a/94489/129

  • Daniel I already have these regular expressions configured in my tool, but if there is a user a little smarter he can circumvent the tool, because those expressions you pass only take the information if the card number is typed with no character between the numbers, and how we work to maintain the security of the information , if we do not create one in this way, we will have leakage of many credit card numbers in the company, I am trying to funnel as much as possible my filter..

2 answers

1

Try that expression:

((\d{16}|(.?\d){16})|(\d{4}(\d{4}){3}))

If you want to accept more than one specific character between the numbers you can simply do so:

([^0-9]*\d{1}){16}

If you want to incorporate in the regex of the question (I do not think necessary, but it is at your discretion):

((\d{16}|[^0-9]*\d{1}){16}|(\d{4}(\d{4}){3}))
  • Samir... Your expression solves my problem, a question, I tested it and this ok, however if I type the number by placing 2 characters or more between the numbers it does not recognize, EX: 40//12001037141112, I can make some adjustment for her to keep picking the number independent of the number of characters typed between the numbers?

  • @Marcussouza , look at the edition of the answer.

  • Samir when I used the expression (( d{16}|[ 0-9]* d{1}){16}|( d{4}( d{4}){3})), it works exactly as I needed it... Just don’t recognize the card numbers of the flags Americacan Express and Dinners Club. If you have any tips thank you. In case Ja has not helped solve my problem 99%..... Thank you very much

  • @Marcussouza, refers to the amount of numbers?

  • If so, you can put this expression: (\d{14,16}|([^0-9]*\d{1}){14,16}|(\d{4}(\d{4}){3})) which accepts 14 to 16 numbers. Or you want another range?

  • Samir sorry the delay, was without access to Internet, I will be validating this new expression and respond as soon as I test.. Thanks

  • Good morning Samir, it worked, Thank you very much for your help.

Show 2 more comments

0

Marcus, to validate the credit card number, I use Algorithm Luhn. Follow the class:

public static class Luhn
{
    public static bool LuhnCheck(this string cardNumber)
    {
        return LuhnCheck(cardNumber.Select(c => c - '0').ToArray());
    }

    private static bool LuhnCheck(this int[] digits)
    {
        return GetCheckValue(digits) == 0;
    }

    private static int GetCheckValue(int[] digits)
    {
        return digits.Select((d, i) => i % 2 == digits.Length % 2 ? ((2 * d) % 10) + d / 5 : d).Sum() % 10;
    }
}
  • 1

    Marcus doesn’t have access to the source code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.