用于在Android中确定信用卡的正则表达式 [英] Regex for determining credit cards in Android

查看:102
本文介绍了用于在Android中确定信用卡的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对regex真的很陌生,但我认为目前我的问题可能不止于此.如标题所述,我正在尝试确定信用卡是否是签证,美国运通卡,万事达卡等.

I'm really new to regex but I think my problem might go beyond that at the moment. As the title says, I'm trying to determine if a credit card is visa, amex, master card etc.

我看了这篇帖子,给出了每种卡类型的正则表达式:

I looked at this post which gave the regular expression for each of the card types:

如何根据以下信息检测信用卡类型数字?

这是我随后使用的代码,但它什么也没做:

This is the code I then used but it doesn't do anything at all:

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函数中的regexp用于根据上述内容确定Discover卡.我已经注意到,除了正则表达式中的"^"以外,我真的无法工作(例如("^ 4"-签证,"^ 6001"发现),但是在编辑以下内容时,这显然是不够的例如,有什么想法吗?我以为这可能是Java的问题,但是我正在运行Java 7

The regexp in the pattern.compile function is for determining Discover cards according to the post above. I've noticed that I really can't get anything to work other than the "^" in regex (i.e ("^4" - visa, "^6001" discover) however this is obviously not sufficient in the case of editing for example. Any ideas what's up? I thought this could be an issue with my Java, but I am running Java 7

我可能想提出一个新问题,但我也想知道,即使用户返回并编辑了号码(xxxx xxxx xxxx xxxx),正则表达式如何可用于获得各种信用卡的正确间距

I might want to make this a new question, but I am also wondering how regex can be used to get spacing correct for various credit cards even if a user goes back and edits the number (xxxx xxxx xxxx xxxx)

从上面添加了调试日志.我的输入是应该与某些信用卡关联的一些数字.目前,我正在使用下面提供的Eagle Eye的代码(也可以用于检测输入的卡类型之一):

Added the DEBUG log from above. My input is a few digits that should associate with certain credit cards. Currently I am using Eagle Eye's code provided below (which should also work for detecting that the input is ONE of the card types):

最终ArrayList listOfPattern = new ArrayList();

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

对于我输入的不同数字,您希望日志发现"会出现几次.上面的日志显示我输入了签证卡和发现卡的前几个数字.

You'd expect the log "discover" to come out a few times for the different digits I've entered. The above log shows me typing in the first few digits of a visa card and a discover card.

发现答案:我只是没有输入足够多的数字来识别该卡!

EDIT ANSWER FOUND: I simply wasn't typing in enough digits for the card to be recognized!

推荐答案

根据线索中的答案之一,可以基于以下数据来验证卡片.

According to one of the answers in the thread, The cards can be validated based on following data.

签证: ^ 4 [0-9] {6,} $ 签证卡号以4开头.

Visa: ^4[0-9]{6,}$ Visa card numbers start with a 4.

万事达卡: ^ 5 [1-5] [0-9] {5,} $ 万事达卡号码从51到55开头,但这只会检测万事达信用卡;还有其他使用万事达卡系统发行的卡不属于此IIN范围.

MasterCard: ^5[1-5][0-9]{5,}$ MasterCard numbers start with the numbers 51 through 55, but this will only detect MasterCard credit cards; there are other cards issued using the MasterCard system that do not fall into this IIN range.

美国运通卡: ^ 3 [47] [0-9] {5,} $ 美国运通卡号以34或37开头.

American Express: ^3[47][0-9]{5,}$ American Express card numbers start with 34 or 37.

晚餐俱乐部: ^ 3(?:0 [0-5] | [68] [0-9])[0-9] {4,} $ Diners Club卡号以300到305、36或38开头.有些Diners Club卡以5开头并有16位数字.这些是Diners Club和万事达卡之间的合资企业,应像万事达卡一样进行处理.

Diners Club: ^3(?:0[0-5]|[68][0-9])[0-9]{4,}$ Diners Club card numbers begin with 300 through 305, 36 or 38. There are Diners Club cards that begin with 5 and have 16 digits. These are a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard.

发现:: ^ 6(?:011 | 5 [0-9] {2})[0-9] {3,} $ 发现卡号开始与6011或65.

Discover: ^6(?:011|5[0-9]{2})[0-9]{3,}$ Discover card numbers begin with 6011 or 65.

JCB: ^(?: 2131 | 1800 | 35 [0-9] {3})[0-9] {3,} $ JCB卡开始用2131、1800或35.

JCB: ^(?:2131|1800|35[0-9]{3})[0-9]{3,}$ JCB cards begin with 2131, 1800 or 35.

因此,您需要为每种情况创建单独的模式.您可以执行以下操作.

So you need to create separate pattern for each case. You can do as follows.

ArrayList<String> listOfPattern=new ArrayList<String>();

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);
}

然后

@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;
      }
   }
}

对于您的最后一个问题,以下主题应为您提供帮助.

And for your last question, The following thread should help you.

在Android的编辑文本中格式化信用卡

例如,如果卡号是16位,则在应用逻辑在4位数字后添加空格后,在处理实际的卡号时需要删除这些空格.然后,只有上述逻辑起作用.

If the card number is 16 digits for example, After applying the logic to add spaces after 4 digits, you need to remove those spaces when you process the actual card number. Then only the above logic will work.

希望这会有所帮助.

这篇关于用于在Android中确定信用卡的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆