我对正则表达式非常陌生,但我认为目前我的问题可能超出了正则表达式的范围。如标题所说,我正在尝试确定信用卡是 Visa、Amex、MasterCard 等类型。
我查看了这篇文章,其中提供了每种卡片类型的正则表达式:
接着我使用了下面这段代码,但它什么也没做:
etCCNum.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d("DEBUG", "beforeTextChanged : "+s);
}
@Override
public void afterTextChanged(Editable s) {
Pattern pattern = Pattern.compile("^6(?:011|5[0-9]{2})[0-9]{3,}$");
Log.d("DEBUG", "afterTextChanged : "+s);
String ccNum = s.toString();
Matcher matcher = pattern.matcher(ccNum);
if(matcher.matches()){
Log.d("DEBUG", "afterTextChanged : discover");
}
}
});
pattern.compile函数中的正则表达式是用来根据上面的帖子确定Discover卡的。我注意到除了正则表达式中的"^"之外,我确实无法让任何其他内容起作用(例如("^4"表示Visa,"^6001"表示Discover),但在编辑时这显然是不够的。有什么想法吗?我以为这可能是我的Java出了问题,但我正在运行Java 7。
也许我想把这变成一个新问题,但我也想知道如何使用正则表达式使各种信用卡的间距正确,即使用户回去编辑数字(xxxx xxxx xxxx xxxx)。
编辑:添加了上面的DEBUG日志。我的输入是几个数字,应该与某些信用卡关联。目前我正在使用Eagle Eye提供的代码(它也适用于检测输入是这些卡类型之一):
final ArrayList listOfPattern=new ArrayList();
String ptVisa = "^4[0-9]{6,}$";
listOfPattern.add(ptVisa);
String ptMasterCard = "^5[1-5][0-9]{5,}$";
listOfPattern.add(ptMasterCard);
String ptAmeExp = "^3[47][0-9]{5,}$";
listOfPattern.add(ptAmeExp);
String ptDinClb = "^3(?:0[0-5]|[68][0-9])[0-9]{4,}$";
listOfPattern.add(ptDinClb);
String ptDiscover = "^6(?:011|5[0-9]{2})[0-9]{3,}$";
listOfPattern.add(ptDiscover);
String ptJcb = "^(?:2131|1800|35[0-9]{3})[0-9]{3,}$";
listOfPattern.add(ptJcb);
etCCNum.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d("DEBUG", "beforeTextChanged : "+s);
}
@Override
public void afterTextChanged(Editable s) {
Log.d("DEBUG", "afterTextChanged : "+s);
String ccNum = s.toString();
for(String p:listOfPattern){
if(ccNum.matches(p)){
Log.d("DEBUG", "afterTextChanged : discover");
break;
}
}
}
});
记录:
01-29 15:16:41.932 26194-26194/com.x.x D/DEBUG﹕ beforeTextChanged :
01-29 15:16:41.933 26194-26194/com.x.x D/DEBUG﹕ afterTextChanged : 4
01-29 15:16:46.815 26194-26194/com.x.x D/DEBUG﹕ beforeTextChanged : 4
01-29 15:16:46.816 26194-26194/com.x.x D/DEBUG﹕ afterTextChanged :
01-29 15:16:50.925 26194-26194/com.x.x D/DEBUG﹕ beforeTextChanged :
01-29 15:16:50.926 26194-26194/com.x.x D/DEBUG﹕ afterTextChanged : 6
01-29 15:16:51.542 26194-26194/com.x.x D/DEBUG﹕ beforeTextChanged : 6
01-29 15:16:51.543 26194-26194/com.x.x D/DEBUG﹕ afterTextChanged : 60
01-29 15:16:51.883 26194-26194/com.x.x D/DEBUG﹕ beforeTextChanged : 60
01-29 15:16:51.883 26194-26194/com.x.x D/DEBUG﹕ afterTextChanged : 600
01-29 15:16:52.928 26194-26194/com.x.x D/DEBUG﹕ beforeTextChanged : 600
01-29 15:16:52.929 26194-26194/com.x.x D/DEBUG﹕ afterTextChanged : 6001
01-29 15:16:55.781 26194-26194/com.x.x D/DEBUG﹕ beforeTextChanged : 6001
01-29 15:16:55.782 26194-26194/com.x.x D/DEBUG﹕ afterTextChanged : 600
01-29 15:16:56.206 26194-26194/com.x.x D/DEBUG﹕ beforeTextChanged : 600
01-29 15:16:56.206 26194-26194/com.x.x D/DEBUG﹕ afterTextChanged : 60
01-29 15:16:57.659 26194-26194/com.x.x D/DEBUG﹕ beforeTextChanged : 60
01-29 15:16:57.660 26194-26194/com.x.x D/DEBUG﹕ afterTextChanged : 605
01-29 15:16:59.297 26194-26194/com.x.x D/DEBUG﹕ beforeTextChanged : 605
01-29 15:16:59.298 26194-26194/com.x.x D/DEBUG﹕ afterTextChanged : 60
01-29 15:16:59.527 26194-26194/com.x.x D/DEBUG﹕ beforeTextChanged : 60
01-29 15:16:59.527 26194-26194/com.x.x D/DEBUG﹕ afterTextChanged : 6
01-29 15:17:00.314 26194-26194/com.x.x D/DEBUG﹕ beforeTextChanged : 6
01-29 15:17:00.314 26194-26194/com.x.x D/DEBUG﹕ afterTextChanged : 65
你会期望"discover"这个日志会针对我输入的不同数字出现几次。上面的日志显示我正在输入Visa信用卡和Discover信用卡的前几位数字。
编辑:我只是没有输入足够多的数字,所以无法识别信用卡!