问题描述
我有一项作业,要求我使用正则表达式验证某些信用卡格式。例如,万事达卡有16位数字,以5开头,后跟15位数字,因此正则表达式如下:
\b5 [0-9] {15} \b
通常是什么下列信用卡格式的表达式?
Diners Club:信用卡有14位数字,以301、302、303、304、305、36或38开头。 / p>
JCB:信用卡的15位数字以2131或1800开头,或者以16位数字以35开头
谢谢!
这应该涵盖所有基础(由RegEx Buddy提供):
^(?:
(?< visa> 4\d {3} [-] * \d {4} [- ] * \d {4} [-] * \d(?:\d {3})?)|
(?< mastercard> 5 [1-5] \d {2} [-] * \d {4} [-] * \d {4} [-] * \d {4})|
(?< discover> 6(?:011 | 5 [ 0-9] {2})[-] * \d {4} [-] * \d {4} [-] * \d {4})|
(?" amex> 3 [47] \d {2} [-] * \d {6} [-] * \d {5})|
(?< diners> 3(?: 0 [0-5] | [68] [0-9])\d [-] * \d {6} [-] * \d {4})|
(?< jcb>(?: 2131 | 1800)[-] * \d {6} [-] * \d {5} | 35\d {2} [-] * \ \d {4} [-] * \d {4} [-] * \d {4})
)$
I have an assignment that requires me to validate certain credit card formats using regular expressions. For example a MasterCard has 16 digits, starts with a 5 and is followed by 15 digits, so the regular expression would be as follows:
\b5[0-9]{15}\b
What would be the regular expressions for the following credit cards formats?
Diners Club: credit card has 14 digits and begins with either 301, 302, 303, 304, 305, 36 or 38.
JCB: credit card has either 15 digits beginning with either 2131 or 1800, or has 16 digits and begins with 35
Thanks!
This should cover all of the bases (provided by RegEx Buddy):
^(?:
(?<visa>4\d{3}[ -]*\d{4}[ -]*\d{4}[ -]*\d(?:\d{3})?) |
(?<mastercard>5[1-5]\d{2}[ -]*\d{4}[ -]*\d{4}[ -]*\d{4}) |
(?<discover>6(?:011|5[0-9]{2})[ -]*\d{4}[ -]*\d{4}[ -]*\d{4}) |
(?<amex>3[47]\d{2}[ -]*\d{6}[ -]*\d{5}) |
(?<diners>3(?:0[0-5]|[68][0-9])\d[ -]*\d{6}[ -]*\d{4}) |
(?<jcb>(?:2131|1800)[ -]*\d{6}[ -]*\d{5}|35\d{2}[ -]*\d{4}[ -]*\d{4}[ -]*\d{4})
)$
这篇关于使用正则表达式验证信用卡格式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!