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

📅  最后修改于: 2021-05-07 05:49:14             🧑  作者: Mango

给定字符串str ,任务是使用正则表达式检查它是否为有效的CVV(卡验证值)数字。
有效的CVV(卡验证码)编号必须满足以下条件:

  1. 它应该包含3或4位数字。
  2. 它应该是介于0到9之间的数字。
  3. 它不应包含任何字母和特殊字符。

例子:

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

  • 获取字符串。
  • 创建一个正则表达式以检查有效的CVV(卡验证值)编号,如下所述:
regex = "^[0-9]{3, 4}$";
  • 在哪里:
    • ^表示字符串的开头。
    • [0-9]表示0-9之间的数字。
    • {3,4}表示具有3或4位数字的字符串。
    • $表示字符串的结尾。
  • 将给定的字符串与正则表达式匹配。在Java,这可以通过使用Pattern.matcher()来完成。
  • 如果字符串与给定的正则表达式匹配,则返回true;否则返回false。

下面是上述方法的实现:

Java
// Java program to validate
// CVV (Card Verification Value)
// number using regex.
import java.util.regex.*;
class GFG {
 
    // Function to validate
    // CVV (Card Verification Value) number.
    // using regular expression.
    public static boolean isValidCVVNumber(String str)
    {
        // Regex to check valid CVV number.
        String regex = "^[0-9]{3,4}$";
 
        // 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
        // uaing 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 = "561";
        System.out.println(isValidCVVNumber(str1));
 
        // Test Case 2:
        String str2 = "5061";
        System.out.println(isValidCVVNumber(str2));
 
        // Test Case 3:
        String str3 = "50614";
        System.out.println(isValidCVVNumber(str3));
 
        // Test Case 4:
        String str4 = "5a#1";
        System.out.println(isValidCVVNumber(str4));
    }
}


Python3
# Python3 program to validate
# CVV (Card Verification Value)
# number using regex.
import re
 
# Function to validate
# CVV (Card Verification Value) number.
# using regular expression.
 
 
def isValidCVVNumber(str):
 
    # Regex to check valid
    # CVV number.
    regex = "^[0-9]{3,4}$"
 
    # Compile the ReGex
    p = re.compile(regex)
 
    # If the string is empty
    # return false
    if(str == None):
        return False
 
    # Return if the string
    # matched the ReGex
    if(re.search(p, str)):
        return True
    else:
        return False
 
# Driver code
 
 
# Test Case 1:
str1 = "561"
print(isValidCVVNumber(str1))
 
# Test Case 2:
str2 = "5061"
print(isValidCVVNumber(str2))
 
# Test Case 3:
str3 = "50614"
print(isValidCVVNumber(str3))
 
# Test Case 4:
str4 = "5a#1"
print(isValidCVVNumber(str4))
 
# This code is contributed by avanitrachhadiya2155


C++
// C++ program to validate the
// CVV (Card Verification Value) number
// using Regular Expression
#include 
#include 
using namespace std;
 
// Function to validate the CVV
// (Card Verification Value) number
bool isValidCVVNumber(string str)
{
 
    // Regex to check valid CVV
    // (Card Verification Value) number
    const regex pattern("^[0-9]{3,4}$");
 
    // If the CVV (Card Verification Value)
    //  number is empty return false
    if (str.empty())
    {
        return false;
    }
 
    // Return true if the CVV
    // (Card Verification Value) number
    // matched the ReGex
    if (regex_match(str, pattern))
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
// Driver Code
int main()
{
    // Test Case 1:
    string str1 = "561";
    cout << isValidCVVNumber(str1) << endl;
 
    // Test Case 2:
    string str2 = "5061";
    cout << isValidCVVNumber(str2) << endl;
 
    // Test Case 3:
    string str3 = "50614";
    cout << isValidCVVNumber(str3) << endl;
 
    // Test Case 4:
    string str4 = "5a#1";
    cout << isValidCVVNumber(str4) << endl;
 
    return 0;
}
 
// This code is contributed by yuvraj_chandra


输出
true
true
false
false