TransWikia.com

Can I get the last 4 digits of a regex credit card number?

Stack Overflow Asked on December 7, 2021

I have this regex pattern for an AMEX Credit Card format – (?<!d)3[47][0-9]{13}(?!d). Is there a way to get the last 4 characters or digits?

I’m trying to mask a credit card in case a credit card format has been inserted in the form. So instead of credit card being inserted, it will be asked something like **** **** **** 1234

2 Answers

It can be 16 digit credit card or more so you can match it using this regex or You can goto This Link and generate your own regex

Regex  (?i)^((d+s+?d+s+?d+s+?)(d{4}))

In here accessing the Group2

                var Creditcardno= // Here put your credit card no
                string reg = @"(?i)^((d+s+d+s+d+s+)(d{4})))";
                Regex r = new Regex(reg, RegexOptions.IgnoreCase);
                Match m = r.Match(Creditcardno);
                int number = m.Groups[2];

You Can get last Four no or anything with grouping the regex.

If you only need last four-digit nothing more

      (.+)([0-9]{4}$) 

use this and from a group1 you can get the last four no.

Answered by ish1104 on December 7, 2021

Here's a pattern to match the last four digits of an American Express card number.

Pattern

3[47]d{2}s?d{6}s?d(d{4})

See the demo here.

Edit: Per request in the discussion we had in the chat, here's a consolidated pattern based on the patterns of most major card networks that you provided (without any research on the specification myself): consolidated pattern

Sources

Answered by Bren on December 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP