gpt4 book ai didi

java - 我有返回类型屏蔽信用卡,我需要从中查找卡类型

转载 作者:行者123 更新时间:2023-12-02 00:48:39 25 4
gpt4 key购买 nike

我将信用卡号码传递为 4242***4242这是被屏蔽的。如何根据屏蔽信用卡号获取卡类型?

    String regVisa = "^4[0-9]{2}(?:[0-9]{3})?$";
String reVisa = "(?:4[0-9]{12}(?:[0-9]{3})?$)";
String regMaster = "^5[1-5][0-9]{14}$";
String regExpress = "^3[47][0-9]{13}$";
String regDiners = "^3(?:0[0-5]|[68][0-9])[0-9]{11}$";
String regDiscover = "^6(?:011|5[0-9]{2})[0-9]{12}$";
String regJCB= "^(?:2131|1800|35\\d{3})\\d{11}$";


if(creditCardNumber.matches(regVisa))
return "visa";
if (creditCardNumber.matches(regMaster))
return "mastercard";
if (creditCardNumber.matches(regExpress))
return "amex";
if (creditCardNumber.matches(regDiners))
return "DINERS";
if (creditCardNumber.matches(regDiscover))
return "discover";
if (creditCardNumber.matches(regJCB))
return "jcb";
if (creditCardNumber.matches(reVisa))
return "VISA";
return "invalid";

最佳答案

也许,你必须为每个设计一些“屏蔽”表达式,例如:

^4[0-9]{2}[0-9]\\*{3}[0-9]{4}$

测试

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class re{

public static void main(String[] args){

final String regex = "^4[0-9]{2}[0-9]\\*{3}[0-9]{4}$";
final String string = "4242***4242\n"
+ "5242***4242";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}

}
}

输出

Full match: 4242***4242
<小时/>

If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.

<小时/>

正则表达式电路

jex.im可视化正则表达式:

enter image description here

关于java - 我有返回类型屏蔽信用卡,我需要从中查找卡类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57877695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com