我的任务是浏览大量字符串值,并删除任何信用卡迹象。因此,如果在字符串中找到任何匹配的内容(其中 n = 数字):
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)
我需要将字符串的那部分替换为[CARD NUMBER REMOVED]
所以,
"Client called and gave credit card details as 1234123412341234, exp 1201, and will be booked next week"
会成为:
"Client called and gave credit card details as `[CARD NUMBER REMOVED]`, exp 1201, and will be booked next week"
我在想 RegEx 会找到这个,但我的正则表达式经验为零,而且有很多模式。而且,我该如何更换那部分?
我可以编写一些遍历每个字符并进行一些规则检查的东西,但这似乎很老套。
有任何想法吗?
我正在尝试这个:
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;
但它似乎没有找到匹配项:
“1234610008918730^^9-11^^代码064^”