寻找或验证信用卡号码寻找或验证信用卡号码寻找或验证信用卡号码寻找或验证信用卡号码
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
寻找或验证信用卡号码
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式范例 » 寻找或验证信用卡号码

示例
正则表达式示例
数字范围
浮点数
电子邮件地址
IP地址
有效日期
数字日期转文本
信用卡号码
匹配完整行
删除重复行
编程
两个相近的单词
陷阱
灾难性回溯
重复次数过多
拒绝服务攻击
使一切选项化
重复捕获组
混合Unicode和8位
更多关于本站
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用进程和语言
正则表达式示例
正则表达式参考
替换字符串参考

寻找或验证信用卡号码

通过几个简单的正则表达式,您可以轻松验证客户在订单表单上输入的有效信用卡号码。您甚至可以确定所使用的信用卡类型。每张卡的发卡机构都有其自己的卡号范围,由前4位数字标识。

您可以使用略有不同的正则表达式来寻找信用卡号码,或者在更大的文档中寻找可能是信用卡号码的数字串行。在安全审计中,这非常有用,以证明您没有不当地暴露客户的财务详情。

我们将从订单表单开始。

去除空格和破折号

第一步是从客户输入的信用卡号码中删除所有非数字字符。实体信用卡中在卡号中有空格以分组数字,这使人类更容易阅读或输入。因此,您的订单表单应该接受带有空格或破折号的卡号。

要从卡号中删除所有非数字字符,只需使用您的编程语言中的“全部替换”函数来搜索正则表达式 [^0-9]+,并将其替换为空。如果您只想替换空格和破折号,可以使用 [ -]+。如果此正则表达式看起来奇怪,请记住,在字符类中,当连字符出现在右括号(或在左括号或否定符号之后)时,它是一个字面值。

如果你想知道为什么要使用 加号:那是为了提高性能。如果输入中有连续的非数字,例如 1===2,那么 [^0-9]+ 就会一次匹配三个等号,并一次性删除它们进行替换。如果没有加号,则需要三次替换。在这种情况下,节省的时间仅为几微秒。但保持正则表达式效率的习惯是很好的。虽然在这里节省的时间很少,但输入额外的加号所需的努力也很少。

在您的订单表格上验证信用卡号码

验证信用卡号码是正则表达式的理想应用场景。它们只是一系列包含 13 到 16 个数字,开头有几个特定的数字来识别卡发行者。您可以使用以下特定的正则表达式,在顾客试图使用您不接受的卡类型时提醒他们,或者将使用不同卡的订单路由到不同的处理器。

  • Visa: ^4[0-9]{12}(?:[0-9]{3})?$ 所有 Visa 信用卡号码都以 4 开头。新卡有 16 位数字。旧卡有 13 位。
  • MasterCard: ^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$ MasterCard 信用卡号码以数字 51 到 55 或数字 2221 到 2720 开头。所有卡号均为 16 位数字。
  • American Express: ^3[47][0-9]{13}$ American Express 信用卡号码以 34 或 37 开头,并且有 15 位数字。
  • Diners Club:^6(?:011|5[0-9]{2})[0-9]{12}$ 发现卡号开头为 6011 或 65。所有卡号皆为 16 位数。
  • JCB: ^(?:2131|1800|35\d{3})\d{11}$ JCB 卡开头为 2131 或 1800 的卡号为 15 位数。JCB 卡开头为 35 的卡号为 16 位数。

如果您只想检查卡号是否看起来有效,而不确定品牌,您可以使用上述六个正则表达式组合,使用 交替。一个 非捕获组 将 锚点 放在交替之外。 自由空间 允许注释和正则表达式适应此页面的宽度。

^(?:4[0-9]{12}(?:[0-9]{3})?          # Visa
 
|  (?:5[1-5][0-9]{2}                # MasterCard
     
| 222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}
 
|  3[47][0-9]{13}                   # American Express
 
|  3(?:0[0-5]|[68][0-9])[0-9]{11}   # Diners Club
 
|  6(?:011|5[0-9]{2})[0-9]{12}      # Discover
 
|  (?:2131|1800|35\d{3})\d{11}      # JCB
)$

这些正则表达式将轻松捕获因客户输入过多或过少位数而无效的卡号。它们不会捕获带有错误数字的卡号。为此,您需要遵循 Luhn 算法,这不能使用正则表达式完成。当然,即使卡号在数学上是有效的,也不代表以该号码发行了卡片或帐户中有钱。正则表达式的好处是,您可以将其放入一点 JavaScript 中,立即检查明显的错误,而不是让客户等待 30 秒以便您的信用卡处理器拒绝订单。如果您的卡片处理器对失败的交易收费,您真的需要实现正则表达式和 Luhn 验证。

在文档中查找信用卡号码

通过两个简单的修改,您可以使用上述任何一个正则表达式在更大的文档中查找卡号。只需将 插入符号和美元符号 替换为 单字边界,如 \b4[0-9]{12}(?:[0-9]{3})?\b。

如果您计划搜索一个大型文档服务器,则简化的正则表达式将加速搜索。除非您的公司对其他用途使用 16 位数字,否则伪阳性很少。正则表达式 \b\d{13,16}\b 将找到任何 13 到 16 位数字的串行。

在搜索满是文档的硬盘时,您不能像验证单个卡号时那样先去掉空格和破折号。要找到带有空格或破折号的卡号,请使用 \b(?:\d[ -

尋找或驗證信用卡號碼
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式範例 » 尋找或驗證信用卡號碼

示例
正則表達式示例
數字範圍
浮點數
電子郵件地址
IP地址
有效日期
數字日期轉文本
信用卡號碼
匹配完整行
刪除重複行
編程
兩個相近的單詞
陷阱
災難性回溯
重複次數過多
拒絕服務攻擊
使一切選項化
重複捕獲組
混合Unicode和8位
更多關於本站
簡介
正則表達式快速入門
正則表達式教程
替換字符串教程
應用程序和語言
正則表達式示例
正則表達式參考
替換字符串參考

尋找或驗證信用卡號碼

通過幾個簡單的正則表達式,您可以輕鬆驗證客戶在訂單表單上輸入的有效信用卡號碼。您甚至可以確定所使用的信用卡類型。每張卡的發卡機構都有其自己的卡號範圍,由前4位數字標識。

您可以使用略有不同的正則表達式來尋找信用卡號碼,或者在更大的文檔中尋找可能是信用卡號碼的數字序列。在安全審計中,這非常有用,以證明您沒有不當地暴露客戶的財務詳情。

我們將從訂單表單開始。

去除空格和破折號

第一步是從客戶輸入的信用卡號碼中刪除所有非數字字符。實體信用卡中在卡號中有空格以分組數字,這使人類更容易閱讀或輸入。因此,您的訂單表單應該接受帶有空格或破折號的卡號。

要從卡號中刪除所有非數字字符,只需使用您的編程語言中的“全部替換”函數來搜索正則表達式 [^0-9]+,並將其替換為空。如果您只想替換空格和破折號,可以使用 [ -]+。如果此正則表達式看起來奇怪,請記住,在字符類中,當連字符出現在右括號(或在左括號或否定符號之後)時,它是一個字面值。

如果你想知道為什麼要使用 加號:那是為了提高性能。如果輸入中有連續的非數字,例如 1===2,那麼 [^0-9]+ 就會一次匹配三個等號,並一次性刪除它們進行替換。如果沒有加號,則需要三次替換。在這種情況下,節省的時間僅為幾微秒。但保持正則表達式效率的習慣是很好的。雖然在這裡節省的時間很少,但輸入額外的加號所需的努力也很少。

在您的訂單表格上驗證信用卡號碼

驗證信用卡號碼是正則表達式的理想應用場景。它們只是一系列包含 13 到 16 個數字,開頭有幾個特定的數字來識別卡發行者。您可以使用以下特定的正則表達式,在顧客試圖使用您不接受的卡類型時提醒他們,或者將使用不同卡的訂單路由到不同的處理器。

  • Visa: ^4[0-9]{12}(?:[0-9]{3})?$ 所有 Visa 信用卡號碼都以 4 開頭。新卡有 16 位數字。舊卡有 13 位。
  • MasterCard: ^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$ MasterCard 信用卡號碼以數字 51 到 55 或數字 2221 到 2720 開頭。所有卡號均為 16 位數字。
  • American Express: ^3[47][0-9]{13}$ American Express 信用卡號碼以 34 或 37 開頭,並且有 15 位數字。
  • Diners Club:^6(?:011|5[0-9]{2})[0-9]{12}$ 發現卡號開頭為 6011 或 65。所有卡號皆為 16 位數。
  • JCB: ^(?:2131|1800|35\d{3})\d{11}$ JCB 卡開頭為 2131 或 1800 的卡號為 15 位數。JCB 卡開頭為 35 的卡號為 16 位數。

如果您只想檢查卡號是否看起來有效,而不確定品牌,您可以使用上述六個正則表達式組合,使用 交替。一個 非捕獲組 將 錨點 放在交替之外。 自由空間 允許註釋和正則表達式適應此頁面的寬度。

^(?:4[0-9]{12}(?:[0-9]{3})?          # Visa
 
|  (?:5[1-5][0-9]{2}                # MasterCard
     
| 222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}
 
|  3[47][0-9]{13}                   # American Express
 
|  3(?:0[0-5]|[68][0-9])[0-9]{11}   # Diners Club
 
|  6(?:011|5[0-9]{2})[0-9]{12}      # Discover
 
|  (?:2131|1800|35\d{3})\d{11}      # JCB
)$

這些正則表達式將輕鬆捕獲因客戶輸入過多或過少位數而無效的卡號。它們不會捕獲帶有錯誤數位的卡號。為此,您需要遵循 Luhn 算法,這不能使用正則表達式完成。當然,即使卡號在數學上是有效的,也不代表以該號碼發行了卡片或帳戶中有錢。正則表達式的好處是,您可以將其放入一點 JavaScript 中,立即檢查明顯的錯誤,而不是讓客戶等待 30 秒以便您的信用卡處理器拒絕訂單。如果您的卡片處理器對失敗的交易收費,您真的需要實現正則表達式和 Luhn 驗證。

在文件中查找信用卡號碼

通過兩個簡單的修改,您可以使用上述任何一個正則表達式在更大的文檔中查找卡號。只需將 插入符號和美元符號 替換為 單字邊界,如 \b4[0-9]{12}(?:[0-9]{3})?\b。

如果您計劃搜索一個大型文檔服務器,則簡化的正則表達式將加速搜索。除非您的公司對其他用途使用 16 位數字,否則偽陽性很少。正則表達式 \b\d{13,16}\b 將找到任何 13 到 16 位數字的序列。

在搜索滿是文件的硬碟時,您不能像驗證單個卡號時那樣先去掉空格和破折號。要找到帶有空格或破折號的卡號,請使用 \b(?:\d[ -

Finding or Verifying Credit Card Numbers
  • 简
  • 繁
  • En
About Regular Expressions » Sample Regular Expressions » Finding or Verifying Credit Card Numbers

Examples
Regular Expressions Examples
Numeric Ranges
Floating Point Numbers
Email Addresses
IP Addresses
Valid Dates
Numeric Dates to Text
Credit Card Numbers
Matching Complete Lines
Deleting Duplicate Lines
Programming
Two Near Words
Pitfalls
Catastrophic Backtracking
Too Many Repetitions
Denial of Service
Making Everything Optional
Repeated Capturing Group
Mixing Unicode & 8-bit
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

Finding or Verifying Credit Card Numbers

With a few simple regular expressions, you can easily verify whether your customer entered a valid credit card number on your order form. You can even determine the type of credit card being used. Each card issuer has its own range of card numbers, identified by the first 4 digits.

You can use a slightly different regular expression to find credit card numbers, or number sequences that might be credit card numbers, within larger documents. This can be very useful to prove in a security audit that you’re not improperly exposing your clients’ financial details.

We’ll start with the order form.

Stripping Spaces and Dashes

The first step is to remove all non-digits from the card number entered by the customer. Physical credit cards have spaces within the card number to group the digits, making it easier for humans to read or type in. So your order form should accept card numbers with spaces or dashes in them.

To remove all non-digits from the card number, simply use the “replace all” function in your scripting language to search for the regex [^0-9]+ and replace it with nothing. If you only want to replace spaces and dashes, you could use [ -]+. If this regex looks odd, remember that in a character class, the hyphen is a literal when it occurs right before the closing bracket (or right after the opening bracket or negating caret).

If you’re wondering what the plus is for: that’s for performance. If the input has consecutive non-digits, such as 1===2, then [^0-9]+ matches the three equals signs at once and deletes them in one replacement. Without the plus, three replacements would be required. In this case, the savings are only a few microseconds. But it’s a good habit to keep regex efficiency in the back of your mind. Though the savings are minimal here, so is the effort of typing the extra plus.

Validating Credit Card Numbers on Your Order Form

Validating credit card numbers is the ideal job for regular expressions. They’re just a sequence of 13 to 16 digits, with a few specific digits at the start that identify the card issuer. You can use the specific regular expressions below to alert customers when they try to use a kind of card you don’t accept, or to route orders using different cards to different processors.

  • Visa: ^4[0-9]{12}(?:[0-9]{3})?$ All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13.
  • MasterCard: ^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$ MasterCard numbers either start with the numbers 51 through 55 or with the numbers 2221 through 2720. All have 16 digits.
  • American Express: ^3[47][0-9]{13}$ American Express card numbers start with 34 or 37 and have 15 digits.
  • Diners Club: ^3(?:0[0-5]|[68][0-9])[0-9]{11}$ Diners Club card numbers begin with 300 through 305, 36 or 38. All have 14 digits. 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.
  • Discover: ^6(?:011|5[0-9]{2})[0-9]{12}$ Discover card numbers begin with 6011 or 65. All have 16 digits.
  • JCB: ^(?:2131|1800|35\d{3})\d{11}$ JCB cards beginning with 2131 or 1800 have 15 digits. JCB cards beginning with 35 have 16 digits.

If you just want to check whether the card number looks valid, without determining the brand, you can combine the above six regexes using alternation. A non-capturing group puts the anchors outside the alternation. Free-spacing allows for comments and for the regex to fit the width of this page.

^(?:4[0-9]{12}(?:[0-9]{3})?          # Visa
 
|  (?:5[1-5][0-9]{2}                # MasterCard
     
| 222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}
 
|  3[47][0-9]{13}                   # American Express
 
|  3(?:0[0-5]|[68][0-9])[0-9]{11}   # Diners Club
 
|  6(?:011|5[0-9]{2})[0-9]{12}      # Discover
 
|  (?:2131|1800|35\d{3})\d{11}      # JCB
)$

These regular expressions will easily catch numbers that are invalid because the customer entered too many or too few digits. They won’t catch numbers with incorrect digits. For that, you need to follow the Luhn algorithm, which cannot be done with a regex. And of course, even if the number is mathematically valid, that doesn’t mean a card with this number was issued or that there’s money in the account. The benefit or the regular expression is that you can put it in a bit of JavaScript to instantly check for obvious errors, instead of making the customer wait 30 seconds for your credit card processor to fail the order. And if your card processor charges for failed transactions, you’ll really want to implement both the regex and the Luhn validation.

Finding Credit Card Numbers in Documents

With two simple modifications, you could use any of the above regexes to find card numbers in larger documents. Simply replace the caret and dollar with a word boundary as in \b4[0-9]{12}(?:[0-9]{3})?\b.

If you’re planning to search a large document server, a simpler regular expression will speed up the search. Unless your company uses 16-digit numbers for other purposes, you’ll have few false positives. The regex \b\d{13,16}\b will find any sequence of 13 to 16 digits.

When searching a hard disk full of files, you can’t strip out spaces and dashes first like you can when validating a single card number. To find card numbers with spaces or dashes in them, use \b(?:\d[ -]*?){13,16}\b. This regex allows any amount of spaces and dashes anywhere in the number. This is really the only way. Visa and MasterCard put digits in sets of 4, while Amex and Discover use groups of 4, 5 and 6 digits. People entering the numbers may have different ideas yet.

©2015-2025 艾丽卡 support@alaica.com