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

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:
Regex current: ((\d{16}|\d{4}(.\d{4}){3})|(\d{4}(\d{4}){3}))
That allows me to read the number of cards when placed in the following ways:

  

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 number of the card, without looking at which field the specific character is typed, some examples of how the card number could appear and which the 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 that I have today works only when the character is typed after the 4th home, I want it to have the same action regardless of where the specific character is.

Can anyone help me? These settings will use DLP , I do not have access to the programming part of it, only to a field where I put the regular expression. p>     

asked by anonymous 19.01.2016 / 18:15

2 answers

1

Try this expression:

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

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

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

If you want to incorporate into the regex of the question (I do not think it's necessary, but it's up to you):

((\d{16}|[^0-9]*\d{1}){16}|(\d{4}(\d{4}){3}))
    
19.01.2016 / 18:31
0

Marcus, to validate the credit card number, I use the Luhn Algorithm. Here's 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;
    }
}
    
19.01.2016 / 18:21