简体   繁体   English

我可以获得正则表达式信用卡号的最后 4 位数字吗?

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

I have this regex pattern for an AMEX Credit Card format - (?<?\d)3[47][0-9]{13}(.?\d).我有这种用于 AMEX 信用卡格式的正则表达式模式 - (?<?\d)3[47][0-9]{13}(.?\d)。 Is there a way to get the last 4 characters or digits?有没有办法获取最后 4 个字符或数字?

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因此,不会插入信用卡,而是会询问诸如 **** **** **** 1234 之类的问题

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它可以是 16 位或更多的信用卡,因此您可以使用此正则表达式匹配它,或者您可以转到此链接并生成您自己的正则表达式

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

In here accessing the Group2在这里访问 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.您可以通过对正则表达式进行分组来获得最后四个 no 或任何内容。

If you only need last four-digit nothing more如果你只需要最后四位数而已

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

use this and from a group 1 you can get the last four no.使用它,从第1组中你可以得到最后四个没有。

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来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM