ColdFusion Quickie: Finding all the credit card numbers in a string

My coworker just asked for a quick way to find all the credit cards in a string. Here is a script I wrote up for it. This does not apply the Luhn algorithm on the results. It simply looks for 16 numbers in a row or 4 sets of 4 digits separated by a space. One nice thing you might like - and might not have seen before - is the use of reMatch, which finds, and returns, all the matches of a regex in a string. I’m sure someone is going to come around and write this 10x better, so consider it a challenge. ;)

<cfsavecontent variable="source"> This is some text with 8902 1248 2381 3821 some crap in it. This line will have 5, not 4, but we should still match the four 9999 8902 1248 2381 3821. And now let's just do 16 in a row 4719209812347891 and then 15 471920981234789 and then 17 - 47192098123478910. Testing my word boundary 1719209812347891. </cfsavecontent> <cfset reg = "\b[[:digit:]]{16,16}\b"> <cfdump var="#reMatch(reg, source)#"> <cfset reg2 = "\b[[:digit:]]{4,4} [[:digit:]]{4,4} [[:digit:]]{4,4} [[:digit:]]{4,4}\b"> <cfdump var="#reMatch(reg2, source)#"> <cfset megareg = "#reg#|#reg2#"> <cfdump var="#reMatch(megareg, source)#">

Note - the first two dumps were simply for testing. If you just wanted the final result you would only run the last call.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments