📌  相关文章
📜  如何使用正则表达式验证印度驾驶执照号码

📅  最后修改于: 2021-04-24 12:11:50             🧑  作者: Mango

给定字符串str ,任务是使用正则表达式检查给定的字符串是否为有效的印度驾驶执照编号。
有效的印度驾驶执照编号必须满足以下条件:

  1. 它应该是16个字符长(包括空格或连字符(-))。
  2. 可以使用以下任何一种格式输入驾驶执照号码:
HR-0619850034761
 OR 
HR06 19850034761
  1. 前两个字符应该是代表状态代码的大写字母。
  2. 接下来的两个字符应该是代表RTO代码的数字。
  3. 接下来的四个字符应为代表许可证颁发年份的数字。
  4. 接下来的七个字符应为0到9之间的任何数字。

注意:在本文中,我们将检查从1900年至2099年颁发的许可证。可以自定义更改许可证颁发年份。

例子:

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

  • 获取字符串。
  • 创建一个正则表达式以检查有效的印度驾驶执照号,如下所述:
  • 在哪里:
    • ^表示字符串的开头。
    • (代表第1组的开始。
    • (代表第2组的开始。
    • [AZ] {2}表示前两个字符应为大写字母。
    • [0-9] {2}表示接下来的两个字符应为数字。
    • )代表群组2的结尾。
    • ()表示空白字符。
    • |代表or。
    • (代表第3组的开始。
    • [AZ] {2}表示前两个字符应为大写字母。
    • 表示连字符。
    • [0-9] {2}表示接下来的两个字符应为数字。
    • )代表群组3的结尾。
    • )代表第1组的结尾。
    • (((19 | 20)[0-9] [0-9])表示从1900年至2099年的年份。
    • [0-9] {7}表示接下来的七个字符应为0-9之间的任何数字。
    • $表示字符串的结尾。
  • 在Java,将给定的字符串与正则表达式进行匹配,这可以通过使用Pattern.matcher()来完成。
  • 如果字符串与给定的正则表达式匹配,则返回true;否则返回false。

下面是上述方法的实现:

Java
// Java program to validate
// Indian driving license number
// using regular expression
 
import java.util.regex.*;
class GFG {
 
    // Function to validate
    // Indian driving license number
    // using regular expression
    public static boolean isValidLicenseNo(String str)
    {
        // Regex to check valid
        // Indian driving license number
        String regex = "^(([A-Z]{2}[0-9]{2})"
                       + "( )|([A-Z]{2}-[0-9]"
                       + "{2}))((19|20)[0-9]"
                       + "[0-9])[0-9]{7}$";
 
        // 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 = "HR-0619850034761";
        System.out.println(isValidLicenseNo(str1));
 
        // Test Case 2:
        String str2 = "UP14 20160034761";
        System.out.println(isValidLicenseNo(str2));
 
        // Test Case 3:
        String str3 = "12HR-37200602347";
        System.out.println(isValidLicenseNo(str3));
 
        // Test Case 4:
        String str4 = "MH27 30123476102";
        System.out.println(isValidLicenseNo(str4));
 
        // Test Case 5:
        String str5 = "GJ-2420180";
        System.out.println(isValidLicenseNo(str5));
    }
}


Python3
# Python program to validate
# Indian driving license number
# using regular expression
 
import re
 
# Function to validate Indian
# driving license number.
def isValidLicenseNo(str):
 
    # Regex to check valid
    # Indian driving license number
    regex = ("^(([A-Z]{2}[0-9]{2})" +
             "( )|([A-Z]{2}-[0-9]" +
             "{2}))((19|20)[0-9]" +
             "[0-9])[0-9]{7}$")
     
    # 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 = "HR-0619850034761"
print(isValidLicenseNo(str1))
 
# Test Case 2:
str2 = "UP14 20160034761"
print(isValidLicenseNo(str2))
 
# Test Case 3:
str3 = "12HR-37200602347"
print(isValidLicenseNo(str3))
 
# Test Case 4:
str4 = "MH27 30123476102"
print(isValidLicenseNo(str4))
 
# Test Case 5:
str5 = "GJ-2420180"
print(isValidLicenseNo(str5))
 
# This code is contributed by avanitrachhadiya2155


C++
// C++ program to validate the
// Indian driving license number
// using Regular Expression
#include 
#include 
using namespace std;
 
// Function to validate the
// Indian driving license number
bool isValidLicenseNo(string str)
{
 
    // Regex to check valid
    // Indian driving license number
    const regex pattern("^(([A-Z]{2}[0-9]{2})( "
                        ")|([A-Z]{2}-[0-9]{2}))"
                        "((19|20)[0-"
                        "9][0-9])[0-9]{7}$");
 
    // If the Indian driving
    // license number is empty return false
    if (str.empty())
    {
        return false;
    }
 
    // Return true if the Indian
    // driving license number
    // matched the ReGex
    if (regex_match(str, pattern))
    {
        return true;
    }
    else {
        return false;
    }
}
 
// Driver Code
int main()
{
    // Test Case 1:
    string str1 = "HR-0619850034761";
    cout << isValidLicenseNo(str1) << endl;
 
    // Test Case 2:
    string str2 = "UP14 20160034761";
    cout << isValidLicenseNo(str2) << endl;
 
    // Test Case 3:
    string str3 = "12HR-37200602347";
    cout << isValidLicenseNo(str3) << endl;
 
    // Test Case 4:
    string str4 = "MH27 30123476102";
    cout << isValidLicenseNo(str4) << endl;
 
    // Test Case 5:
    string str5 = "GJ-2420180";
    cout << isValidLicenseNo(str5) << endl;
 
    return 0;
}
 
// This code is contributed by yuvraj_chandra


输出
true
true
false
false
false