How do I search for a 16-digit long string (Luhn algorithm) using notepad++?

2k views Asked by At

I have a large text file containing credit card numbers, I've been told I need to look through this file and then find possible credit card numbers, I've been using Notepad ++ 'Find in Files' regular expression search mode using this simple expression: 4\d{15} (this searches for a 16 digit long string starting with 4 which is usually a VISA Debit/Credit) and then I copy and paste it into a Credit Card Validation script.

Is there anyway to create an expression that will search for 16 digit long strings starting with 4, and checks to see if it uses the Luhn Algorithm (makes sure it is valid).

This is the Luhn Algorithm:

1) Starting with the second to last digit and moving left, double the value of all the alternating digits.

2) Starting from the left, take all the unaffected digits and add them to the results of all the individual digits from step 1. If the results from any of the numbers from step 1 are double digits, make sure to add the two numbers first (i.e. 18 would yield 1+8). Basically, your equation will look like a regular addition problem that adds every single digit.

3) The total from step 2 must end in zero for the credit-card number to be valid.

Source: http://www.webopedia.com/TERM/L/Luhn_formula.html

1

There are 1 answers

0
Lasse V. Karlsen On BEST ANSWER

Here's a simple LINQPad program that extracts all 16-digit numbers that starts with a 4 from the file:

void Main()
{
    const string inputFileName = @"d:\temp\input.txt";
    const string outputFileName = @"d:\temp\output.txt";

    string input = File.ReadAllText(inputFileName);
    var matches =
        from Match ma in Regex.Matches(input, @"\d+")
        let number = ma.Value
        where number.Length == 16 && number.StartsWith("4")
        select number;

    var creditCardNumbers =
        from match in matches
        where IsCreditCardNumber(match)
        select match;

    File.WriteAllLines(outputFileName, creditCardNumbers);
}

public static bool IsCreditCardNumber(string number)
{
    // validate luhn checksum here
    return true;
}