Discover 信用卡的正则表达式 [英] Regex for Discover credit card

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

问题描述

我已阅读这个问题,但对于 Discover 卡,起始数字是 6011, 622126-622925, 644-649, 65 而不是 6011, 65.(来源)

对于 Discover 卡,我从那个问题 ^6(?:011|5[0-9]{2})[0-9]{12}$

我修改了它以涵盖6011644-649&65 但对于 622126-622925,构建正则表达式很难,因为我的正则表达式技能很差.

到目前为止我有这个正则表达式 6(?:011|5[0-9]{2}|[4][4-9][0-9]|[2]{2}[1-9])[0-9]{2}$,但它只检查 622[1-9]**.

如何修改它以使其仅在 622126-622925 之间接受 622*** 案例?

解决方案

这是你的正则表达式 (demo):

^6(?:011\d{12}|5\d{14}|4[4-9]\d{13}|22(?:1(?:2[6-9]|[3-9]\d)|[2-8]\d{2}|9(?:[01]\d|2[0-5]))\d{10})$

不用说,我不会说这很漂亮或易于维护.我建议将数字解析为整数并使用您的编程语言进行检查.

您还应该使用Luhn 算法来检查信用卡号是否有效,同时你可以理论上用正则表达式做到这一点,它会比这更糟糕.

<小时>

请允许我逐步向您展示我是如何到达这个怪物的.首先,您可以通过以下方式匹配每个范围:

6011 # 匹配 601165 # 匹配 6564[4-9] # 匹配 644-649622(1(2[6-9]|[3-9]\d)|[2-8]\d{2}|9([01]\d|2[0-5]))# 匹配 622126-622925

现在,您想匹配其余的数字:

6011\d{12} # 匹配 6011 + 12 位数字65\d{14} # 匹配 65 + 14 位数字64[4-9]\d{13} # 匹配 644-649 + 13 位数字622(1(2[6-9]|[3-9]\d)|[2-8]\d{2}|9([01]\d|2[0-5]))\d{10}# 匹配 622126-622925 + 10 位数字

现在您可以组合所有四个,并添加行首和行尾锚点:

^( # 匹配字符串的开头和打开的组6011\d{12}|# 匹配 6011 + 12 位数字65\d{14}|# 匹配 65 + 14 位数字64[4-9]\d{13}|# 匹配 644-649 + 13 位数字622(1(2[6-9]|[3-9]\d)|[2-8]\d{2}|9([01]\d|2[0-5]))\d{10}# 匹配 622126-622925 + 10 位数字)$ # 关闭组并匹配字符串的结尾

上面的最终产品是先前正则表达式的稍微压缩版本,我还使组不可捕获(这就是那些 ?: 的用途).

I have read through this question, but for Discover card, the starting digits are 6011, 622126-622925, 644-649, 65 instead of just 6011, 65. (Source)

For Discover cards, I picked up this regex from that question ^6(?:011|5[0-9]{2})[0-9]{12}$

I modified it to cover 6011, 644-649& 65 but for 622126-622925, building regex is hard cuz of my poor regex skills.

I have this regex so far 6(?:011|5[0-9]{2}|[4][4-9][0-9]|[2]{2}[1-9])[0-9]{2}$, but it only checks for 622[1-9]**.

How do I modify it so that it accepts only between 622126-622925 for 622*** case?

解决方案

Here's your regex (demo):

^6(?:011\d{12}|5\d{14}|4[4-9]\d{13}|22(?:1(?:2[6-9]|[3-9]\d)|[2-8]\d{2}|9(?:[01]\d|2[0-5]))\d{10})$

Needless to say, I won't exactly call this pretty or easy to maintain. I would recommend parsing the number as an integer and using your programming language to do the checks.

You should also use Luhn algorithm to check if the credit card number is valid, and while you could theoretically do this with regex, it would many times worse than this.


Allow me to show you how I arrived at this monstrosity, step by step. First, here is how you match each of those ranges:

6011        # matches 6011
65          # matches 65
64[4-9]     # matches 644-649
622(1(2[6-9]|[3-9]\d)|[2-8]\d{2}|9([01]\d|2[0-5]))  
            # matches 622126-622925

Now, you want to match the rest of the digits:

6011\d{12}        # matches 6011 + 12 digits
65\d{14}          # matches 65 + 14 digits
64[4-9]\d{13}     # matches 644-649 + 13 digits
622(1(2[6-9]|[3-9]\d)|[2-8]\d{2}|9([01]\d|2[0-5]))\d{10}
                  # matches 622126-622925 + 10 digits

Now you can combine all four, and add start and end of line anchors:

^(                  # match start of string and open group
 6011\d{12}|        # matches 6011 + 12 digits
 65\d{14}|          # matches 65 + 14 digits
 64[4-9]\d{13}|     # matches 644-649 + 13 digits
 622(1(2[6-9]|[3-9]\d)|[2-8]\d{2}|9([01]\d|2[0-5]))\d{10}
                    # matches 622126-622925 + 10 digits
)$                  # close group and match end of string

The final product above is a slightly compacted version of the previous regex, and I also made groups non-capturing (that's what those ?: are for).

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

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