0

I have been tasked with going through a large list of string values, and removing any sign of credit cards. So, if, within the string, I find anything that matches (where n = numerical):

nnnn nnnn nnnn nnnn (4 x 4 numerical)
nnnnnnnnnnnnnnnn (16 numerical)
nnnn-nnnn-nnnn-nnnn (Hyphened)
nnnn nnnnnn nnnn (American express?)
nnnnnnnnnnnnnn (AX, no spaces)
nnnn-nnnnnn-nnnn (AX, Hyphened)

I need to replace that part of the string with [CARD NUMBER REMOVED]

So,

"Client called and gave credit card details as 1234123412341234, exp 1201, and will be booked next week"

would become:

"Client called and gave credit card details as `[CARD NUMBER REMOVED]`, exp 1201, and will be booked next week"

I'm thinking RegEx would FIND this, but I have zero regex experience, and there are many patterns. And, how do I replace that portion?

I could write something that iterates through each char and does some rule checking, but that seems hacky.

Any ideas?

I am trying this:

    const string pattern = @"^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$";
    const string replacement = "[CARD DETAILS REMOVED]";
    var rgx = new Regex(pattern);
    string cleansedText = rgx.Replace(UncleansedText, replacement);
    return cleansedText;

But it doesn't seem to find a match in this:

"1234610008918730^^9-11^^Code 064^"

Craig
  • 18,074
  • 38
  • 147
  • 248

3 Answers3

3

Here's a similar question, but no selected answer. The top voted one however suggested a web site with the following regex:

\b(?:\d[ -]*?){13,16}\b
Community
  • 1
  • 1
austin
  • 5,816
  • 2
  • 32
  • 40
0
myString = Regex.Replace(myString, 
    @"(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})", 
    "`[CARD NUMBER REMOVED]`");

Regex for credit card taken from here.

Community
  • 1
  • 1
Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
  • Regex isn't static, so I tried this, with the version I have. See original question - but I must be doing somethign wrong? – Craig Nov 12 '12 at 03:03
  • @Craig the `Regex` class has a static method [Replace](http://msdn.microsoft.com/en-us/library/e7f5w83z.aspx). I tried this in VS, it works. – Yuriy Faktorovich Nov 12 '12 at 06:10
0

Get ready, because this one's a doosy!

Regex regexObj = new Regex(@"(?:(?<visa>4[ -.,/\\]*(?:\d[ -.,/\\]*){11}(?:(?:\d[ -.,/\\]*){3})?\d)|(?<mastercard>5[ -.,/\\]*[1-5](?:[ -.,/\\]*[0-9]){14})|(?<discover>6[ -.,/\\]*(?:0[ -.,/\\]*1[ -.,/\\]*1|5[ -.,/\\]*\d[ -.,/\\]*\d)(?:[ -.,/\\]*[0-9]){12})|(?<amex>3[ -.,/\\]*[47](?:[ -.,/\\]*[0-9]){13})|(?<diners>3[ -.,/\\]*(?:0[ -.,/\\]*[0-5]|[68][ -.,/\\]*[0-9])(?:[ -.,/\\]*[0-9]){11}) |(?<jcb>(?:2[ -.,/\\]*1[ -.,/\\]*3[ -.,/\\]*1|1[ -.,/\\]*8[ -.,/\\]*0[ -.,/\\]*0|3[ -.,/\\]*5(?:[ -.,/\\]*[0-9]){3})(?:[ -.,/\\]*[0-9]){11}))");
resultString = regexObj.Replace(subjectString, "[CARD DETAILS REMOVED]");

This is modified slightly from a RegexBuddy library. What I added was the ability to separate the numbers using spaces, dashes, periods, commas, forward slashes and back slashes in any combination and in any grouping style.

Note: this one will match the example you gave above.

Also, it will not match numbers that have the same number of digits but don't appear to be valid credit card numbers.

Example: "Please call the client on 5555 4141, 5555 3214 or just email him at ourclient@example.com"

Iain Fraser
  • 6,578
  • 8
  • 43
  • 68