📌  相关文章
📜  如何使用正则表达式验证Visa卡号

📅  最后修改于: 2021-05-06 21:29:39             🧑  作者: Mango

给定字符串str ,任务是使用正则表达式检查给定的字符串是否为有效的Visa卡号。
有效的Visa卡号必须满足以下条件:

  1. 它应该是13或16位数字,新卡有16位数字,旧卡有13位数字。
  2. 它应该以4开头。
  3. 如果卡片有13位数字,则接下来的12位数字应为0-9之间的任意数字。
  4. 如果卡片有16位数字,则接下来的15位数字应为0-9之间的任意数字。
  5. 它不应包含任何字母和特殊字符。

例子:

方法:想法是使用正则表达式解决此问题。可以按照以下步骤计算答案。

  1. 获取字符串。
  2. 创建一个正则表达式以检查有效的Visa卡号,如下所述:
regex = "^4[0-9]{12}(?:[0-9]{3})?$";
  1. 在哪里:
    • ^表示字符串的开头。
    • 4表示字符串应以4开头。
    • [0-9] {12}表示接下来的十二个数字应介于0-9之间。
    • (代表小组的开始。
    • ?代表0或1次。
    • [0-9] {3}表示接下来的三位数应在0-9之间。
    • )代表群组的结尾。
    • ?代表0或1次。
    • $表示字符串的结尾。
  2. 将给定的字符串与正则表达式匹配。
    在Java,这可以通过使用Pattern.matcher()来完成。
    在C++中,这可以通过使用regex_match()来完成。
  3. 如果字符串与给定的正则表达式匹配,则返回true;否则返回false。

下面是上述方法的实现:

C++
// C++ program to validate
// Visa Card number
// using Regular Expression
#include 
#include 
using namespace std;
 
// Function to validate Visa Card number
bool isValidVisaCardNo(string str)
{
 
  // Regex to check valid Visa Card number
  const regex pattern("^4[0-9]{12}(?:[0-9]{3})?$");
 
  // If the Visa Card number
  // is empty return false
  if (str.empty())
  {
     return false;
  }
 
  // Return true if the Visa Card number
  // matched the ReGex
  if(regex_match(str, pattern))
  {
    return true;
  }
  else
  {
    return false;
  }
}
 
// Driver Code
int main()
{
   
  // Test Case 1:
  string str1 = "4155279860457";
  cout << isValidVisaCardNo(str1) << endl;
 
  // Test Case 2:
  string str2 = "4155279860457201";
  cout << isValidVisaCardNo(str2) << endl;
 
  // Test Case 3:
  string str3 = "4155279";
  cout << isValidVisaCardNo(str3) << endl;
 
  // Test Case 4:
  string str4 = "6155279860457";
  cout << isValidVisaCardNo(str4) << endl;
 
  // Test Case 5:
  string str5 = "415a27##60457";
  cout << isValidVisaCardNo(str5) << endl;
 
  return 0;
}
 
// This code is contributed by yuvraj_chandra


Java
// Java program to validate
// Visa Card number
// using regular expression
import java.util.regex.*;
class GFG {
 
    // Function to validate
    // Visa Card number.
    // using regular expression.
    public static boolean
    isValidVisaCardNo(String str)
    {
        // Regex to check valid.
        // Visa Card number
        String regex = "^4[0-9]{12}(?:[0-9]{3})?$";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
 
        // If the string is empty
        // return false
        if (str == null) {
            return false;
        }
 
        // Find match between given string
        // and regular expression
        // using Pattern.matcher()
 
        Matcher m = p.matcher(str);
 
        // Return if the string
        // matched the ReGex
        return m.matches();
    }
 
    // Driver code
    public static void main(String args[])
    {
 
        // Test Case 1:
        String str1 = "4155279860457";
        System.out.println(
            isValidVisaCardNo(str1));
 
        // Test Case 2:
        String str2 = "4155279860457201";
        System.out.println(
            isValidVisaCardNo(str2));
 
        // Test Case 3:
        String str3 = "4155279";
        System.out.println(
            isValidVisaCardNo(str3));
 
        // Test Case 4:
        String str4 = "6155279860457";
        System.out.println(
            isValidVisaCardNo(str4));
 
        // Test Case 5:
        String str5 = "415a27##60457";
        System.out.println(
            isValidVisaCardNo(str5));
    }
}


Python3
# Python3 program to validate Visa
# Card number using regular expression
import re
 
# Function to validate Visa Card
# number using regular expression.
def isValidVisaCardNo(string):
     
    # Regex to check valid Visa
    # Card number
    regex = "^4[0-9]{12}(?:[0-9]{3})?$";
 
    # Compile the ReGex
    p = re.compile(regex);
     
    # If the string is empty
    # return false
    if (string == ''):
        return False;
         
    # Pattern class contains matcher()
    # method to find matching between
    # given string and regular expression.
    m = re.match(p, string);
     
    # Return True if the string
    # matched the ReGex else False
    if m is None:
        return False
    else:
        return True
 
# Driver code
if __name__ == "__main__":
     
    # Test Case 1
    str1 = "4155279860457";
    print(isValidVisaCardNo(str1));
     
    # Test Case 2
    str2 = "4155279860457201";
    print(isValidVisaCardNo(str2));
     
    # Test Case 3
    str3 = "4155279";
    print(isValidVisaCardNo(str3));
     
    # Test Case 4
    str4 = "6155279860457";
    print(isValidVisaCardNo(str4));
     
    # Test Case 5
    str5 = "415a27##60457";
    print(isValidVisaCardNo(str5));
     
# This code is contributed by AnkitRai01


输出:
true
true
false
false
false