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

📅  最后修改于: 2021-05-06 19:01:54             🧑  作者: Mango

字母数字字符定字符串str,任务是检查字符串是否是一个有效的PAN(永久帐号)卡号码或不使用正则表达式。
有效的PAN卡号必须满足以下条件:

  1. 它应该是十个字符长。
  2. 前五个字符应为任何大写字母。
  3. 接下来的四个字符应为0到9之间的任何数字。
  4. 最后(第十个)字符应为任何大写字母。
  5. 它不应包含任何空格。

例子:

方法:可以使用正则表达式解决此问题。

  • 获取字符串。
  • 创建一个正则表达式以验证PAN卡号,如下所述:
regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}";
  • 在哪里:
    • [AZ] {5}表示前五个大写字母,可以是A到Z。
    • [0-9] {4}代表可以为0-9的四个数字。
    • [AZ] {1}表示一个大写字母,可以是A到Z。
  • 在Java,使用regex匹配给定的字符串,这可以使用Pattern.matcher()来完成。
  • 如果字符串与给定的正则表达式匹配,则返回true;否则返回false。

下面是上述方法的实现:

Java
// Java program to validate the
// PAN Card number using Regular Expression
 
import java.util.regex.*;
 
class GFG
{
 
    // Function to validate the PAN Card number.
    public static boolean isValidPanCardNo(String panCardNo)
    {
 
        // Regex to check valid PAN Card number.
        String regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
 
        // If the PAN Card number
        // is empty return false
        if (panCardNo == null)
        {
            return false;
        }
 
        // Pattern class contains matcher() method
        // to find matching between given
        // PAN Card number using regular expression.
        Matcher m = p.matcher(panCardNo);
 
        // Return if the PAN Card number
        // matched the ReGex
        return m.matches();
    }
 
    // Driver Code.
    public static void main(String args[])
    {
 
        // Test Case 1:
        String str1 = "BNZAA2318J";
        System.out.println(isValidPanCardNo(str1));
 
        // Test Case 2:
        String str2 = "23ZAABN18J";
        System.out.println(isValidPanCardNo(str2));
 
        // Test Case 3:
        String str3 = "BNZAA2318JM";
        System.out.println(isValidPanCardNo(str3));
 
        // Test Case 4:
        String str4 = "BNZAA23184";
        System.out.println(isValidPanCardNo(str4));
 
        // Test Case 5:
        String str5 = "BNZAA 23184";
        System.out.println(isValidPanCardNo(str5));
    }
}


Python3
# Python3 program to validate the
# PAN Card number using Regular
# Expression
import re
 
# Function to validate the
# PAN Card number.
 
 
def isValidPanCardNo(panCardNo):
 
    # Regex to check valid
    # PAN Card number
    regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}"
 
    # Compile the ReGex
    p = re.compile(regex)
 
    # If the PAN Card number
    # is empty return false
    if(panCardNo == None):
        return False
 
    # Return if the PAN Card number
    # matched the ReGex
    if(re.search(p, panCardNo) and
       len(panCardNo) == 10):
        return True
    else:
        return False
 
# Driver Code.
 
 
# Test Case 1:
str1 = "BNZAA2318J"
print(isValidPanCardNo(str1))
 
# Test Case 2:
str2 = "23ZAABN18J"
print(isValidPanCardNo(str2))
 
# Test Case 3:
str3 = "BNZAA2318JM"
print(isValidPanCardNo(str3))
 
# Test Case 4:
str4 = "BNZAA23184"
print(isValidPanCardNo(str4))
 
# Test Case 5:
str5 = "BNZAA 23184"
print(isValidPanCardNo(str5))
 
# This code is contributed by avanitrachhadiya2155


C++
// C++ program to validate the
// PAN Card number using Regular
//  Expression
 
#include 
#include 
using namespace std;
 
// Function to validate the
// PAN Card number.
bool isValidPanCardNo(string panCardNo)
{
 
    // Regex to check valid
    // PAN Card number.
    const regex pattern("[A-Z]{5}[0-9]{4}[A-Z]{1}");
 
    // If the PAN Card number
    // is empty return false
    if (panCardNo.empty()) {
        return false;
    }
 
    // Return true if the PAN Card number
    // matched the ReGex
    if (regex_match(panCardNo, pattern))
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
// Driver Code
int main()
{
    // Test Case 1:
    string str1 = "BNZAA2318J";
    cout << isValidPanCardNo(str1) << endl;
 
    // Test Case 2:
    string str2 = "23ZAABN18J";
    cout << isValidPanCardNo(str2) << endl;
 
    // Test Case 3:
    string str3 = "BNZAA2318JM";
    cout << isValidPanCardNo(str3) << endl;
 
    // Test Case 4:
    string str4 = "BNZAA23184";
    cout << isValidPanCardNo(str4) << endl;
 
    // Test Case 5:
    string str5 = "BNZAA 23184";
    cout << isValidPanCardNo(str5) << endl;
 
    return 0;
}
 
// This code is contributed by yuvraj_chandra


输出
true
false
false
false
false