简体   繁体   English

如何在.net中屏蔽信用卡号的前6位和后4位

[英]How to mask first 6 and last 4 digits for a credit card number in .net

I'm very new to regex And I'm trying to use a regular expression to turn a credit card number which will be part of a conversation into something like 492900******2222 我是regex的新手,并且我正尝试使用正则表达式将将成为会话一部分的信用卡号码转换为类似492900 ****** 2222的内容

As it can come from any conversation it might contain string next to it or might have an inconsistent format, so essentially all of the below should be formatted to the example above: 由于它可能来自任何会话,因此可能在其旁边包含字符串或格式可能不一致,因此,基本上所有以下内容都应格式化为上面的示例:

  • hello my number is492900001111222 你好我的号码是492900001111222
  • number is 4929000011112222ok? 号是4929000011112222 OK?
  • 4929 0000 1111 2222 4929 0000 1111 2222
  • 4929-0000-1111-2222 4929-0000-1111-2222

It needs to be a regular expression which extracts the capture group of which I will then be able to use a MatchEvaluator to turn all digits (excluding non digits) which are not the first 6 and last 4 into a * 它必须是一个正则表达式,用于提取捕获组,然后我将能够使用MatchEvaluator将所有不是前6位和后4位的数字(不包括非数字)转换为*

I've seen many examples here on stack overflow for PHP and JS but none which helps me resolve this issue. 我在这里看到了很多关于PHP和JS堆栈溢出的示例,但是没有一个可以帮助我解决此问题。

Any guidance will be appreciated 任何指导将不胜感激

UPDATE UPDATE

I need to expand upon an existing implementation which uses MatchEvaluator to mask each character that is not the first 6 or last 4 and ideally I dont want to change the MatchEvaluator and just make the masking flexible based on the regular expression, see this for an example https://dotnetfiddle.net/J2LCo0 我需要扩展现有的使用MatchEvaluator来掩盖不是前6个或后4个字符的每个实现的实现,理想情况下,我不想更改MatchEvaluator,而只是基于正则表达式使屏蔽变得灵活,请参见示例https://dotnetfiddle.net/J2LCo0

UPDATE 2 更新2

@Matt.G and @CAustin answers do resolve what I asked for but I am hitting another barrier where I cant have it be so strict. @ Matt.G和@CAustin的答案确实可以解决我的要求,但是我遇到了另一个我无法做到如此严格的障碍。 The final captured group needs to only take into account the digits and as such maintain the format of the input text. 最终捕获的组只需要考虑数字,并因此保持输入文本的格式。 So for example: 因此,例如:

If some types in my card number is 99 9988 8877776666 the output from the evaluation should be 99 9988 ******666666 如果我的卡号中的某些类型是99 9988 8877776666,则评估的输出应为99 9988 ****** 666666

OR my card number is 9999-8888-7777-6666 it should output 9999-88**-****-6666. 或我的卡号是9999-8888-7777-6666,它应该输出9999-88 **-****-6666。

Is this possible? 这可能吗?

Changed the list to include items that are in my unit tests https://dotnetfiddle.net/tU6mxQ 更改了列表以包括我的单元测试中的项目https://dotnetfiddle.net/tU6mxQ

Try Regex: (?<=\\d{4}\\d{2})\\d{2}\\d{4}(?=\\d{4})|(?<=\\d{4}( |-)\\d{2})\\d{2}\\1\\d{4}(?=\\1\\d{4}) 尝试Regex: (?<=\\d{4}\\d{2})\\d{2}\\d{4}(?=\\d{4})|(?<=\\d{4}( |-)\\d{2})\\d{2}\\1\\d{4}(?=\\1\\d{4})

Regex Demo 正则表达式演示

C# Demo C#示范

Explanation: 说明:

2 alternative regexes
(?<=\d{4}\d{2})\d{2}\d{4}(?=\d{4}) - to handle cardnumbers without any separators (- or <space>)
(?<=\d{4}( |-)\d{2})\d{2}\1\d{4}(?=\1\d{4}) - to handle cardnumbers with separator (- or <space>)

1st Alternative (?<=\d{4}\d{2})\d{2}\d{4}(?=\d{4})
    Positive Lookbehind (?<=\d{4}\d{2}) - matches text that has 6 digits immediately behind it
    \d{2} matches a digit (equal to [0-9])
        {2} Quantifier — Matches exactly 2 times
    \d{4} matches a digit (equal to [0-9])
        {4} Quantifier — Matches exactly 4 times
    Positive Lookahead (?=\d{4}) - matches text that is followed immediately by 4 digits
        Assert that the Regex below matches
            \d{4} matches a digit (equal to [0-9])
            {4} Quantifier — Matches exactly 4 times

2nd Alternative (?<=\d{4}( |-)\d{2})\d{2}\1\d{4}(?=\1\d{4})

    Positive Lookbehind (?<=\d{4}( |-)\d{2}) - matches text that has (4 digits followed by a separator followed by 2 digits) immediately behind it
    1st Capturing Group ( |-) - get the separator as a capturing group, this is to check the next occurence of the separator using \1
    \1 matches the same text as most recently matched by the 1st capturing group (separator, in this case)
    Positive Lookahead (?=\1\d{4}) - matches text that is followed by separator and 4 digits

If performance is a concern, here's a pattern that only goes through 94 steps, instead of the other answer's 473, by avoiding lookaround and alternation: 如果您担心性能问题,那么可以通过避免环顾四周和轮流选择的方式,仅通过94个步骤(而不是其他答案的473个步骤)进行操作:

\\d{4}[ -]?\\d{2}\\K\\d{2}[ -]?\\d{4}

Demo: https://regex101.com/r/0XMluq/4 演示: https : //regex101.com/r/0XMluq/4

Edit: In C#'s regex flavor, the following pattern can be used instead, since C# allows variable length lookbehind. 编辑:在C#的正则表达式中,可以使用以下模式代替,因为C#允许在后面进行变长查找。

(?<=\\d{4}[ -]?\\d{2})\\d{2}[ -]?\\d{4}

Demo 演示

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

相关问题 我可以获得正则表达式信用卡号的最后 4 位数字吗? - Can I get the last 4 digits of a regex credit card number? 在日志文件中屏蔽信用卡号 - Mask credit card number in log file 如何格式化数字以使前6位和后4位不隐藏 - How to format a number such that first 6 digits and last 4 digits are not hidden .NET正则表达式以字符串形式查找信用卡号 - .Net regex to look for a credit card number in a string 屏蔽除字符串的前 6 位和后 4 位以外的所有数字(长度不同) - mask all digits except first 6 and last 4 digits of a string( length varies ) C#Windows应用程序(非wpf)在输入的文本框中屏蔽信用卡号,然后验证和处理信用卡号 - C# Windows Application (not wpf) mask credit card number in text box as being entered followed by validation and processing of credit card number 如何在ASP.NET文本框中仅允许信用卡/借记卡号格式 - How to allow only Credit/Debit card number format in ASP.NET textbox 如何检查仅信用卡号有效而不是借记卡..? - How to check Only Credit card number is valid not Debit card..? 通过添加第一个和最后一个数字来减少给定的数字 - Reduce the given number by adding first and last digits 获取4位数字的信用卡号Authorize.net - Get 4 digit credit card number Authorize.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM