如何使用正则表达式验证CVV数字

2021年4月26日16:27:24 发表评论 1,057 次浏览

本文概述

给定字符串str,任务是检查它是否是一个有效的CVV(卡片验证值)数字或不使用正则表达式

有效的CVV(卡验证码)数字必须满足以下条件:

  1. 它应该包含3或4位数字。
  2. 它应该在0-9之间。
  3. 它不应包含任何字母和特殊字符。

例子:

输入:str =" 561"
输出:true
说明:
给定的字符串满足上述所有条件。因此, 它是有效的CVV(卡验证值)数字。
输入:str =" 50614"
输出:false
说明:
给定的字符串有五位数字。因此, 它不是有效的CVV(卡验证值)号码。
输入:str =" 5a#1"
输出:false
说明:给定的字符串包含字母和特殊字符。因此, 它不是有效的CVV(卡验证值)号码。

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

  • 获取字符串。
  • 创建一个正则表达式以检查有效的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

输出如下:

true
true
false
false

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: