使用正则表达式验证印度车辆牌照
在本问题中,我们将使用正则表达式验证印度车辆牌照。
正则表达式是由不同字符创建的搜索模式,我们可以用它来匹配给定字符串中的特定模式。
问题陈述 - 我们给定一个表示印度车辆牌照的字符串。我们需要使用正则表达式来验证给定的字符串。
示例
Input: num1 = "GJ 03 AY 1097" Output: Yes
解释 - 给定的车辆牌照有效。
Input: num2 = "TN 1A3 PZ 1287" Output: No
解释 - 给定的车牌无效,因为有 '1A3'。
Input: num2 = "DL 29 KJX 0001" Output: No
解释 - 给定的车牌无效,因为有 'KJX'。
有效的印度车辆牌照的示例为 GJ 03 AY 1097。
车牌包含 2 个州编号字符。
之后,它包含 2 位数字,代表区号。
接下来,它包含 1 或 2 个字母字符。
最后,它包含 4 位数字。
用户可以遵循以下正则表达式来验证印度车辆牌照。
"^[A-Z]{2}[ -]?[0-9]{2}[ -]?[A-Z]{1,2}[ -]?[0-9]{4}$"
让我们理解一下正则表达式。
^ − 它表示车牌的开头。
[A - Z]{2} − 它应该包含 2 个大写字母字符。
[ -]? − 之后,它可能包含空格或连字符。
[0-9]{2}[ -]? − 它应该包含 2 位数字,代表区号。
[A-Z]{1,2} − 它应该包含 1 或 2 个字母字符。
[0-9]{4}$ − 最后,它应该包含 4 位数字。
算法
步骤 1 − 定义名为 patt 的正则表达式模式。
步骤 2 − 如果字符串为空,则返回 'No'。
步骤 3 − 使用 regex_match() 方法使用 'patt' 验证车牌字符串。
步骤 4 − 如果 regex_match() 方法返回 true,则从函数返回 'yes'。否则,从函数返回 'No'。
示例
以下是各种编程语言中上述算法的程序
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <string.h>
// Function to check if a number plate is valid
char* checkForNumberPlate(char* numPlate) {
// Defining the regular expression
regex_t patt;
int reti = regcomp(&patt, "^[A-Z]{2}[ -]?[0-9]{2}[ -]?[A-Z]{1,2}[ -]?[0-9]{4}$", REG_EXTENDED);
if (reti) {
fprintf(stderr, "Could not compile regex\n");
exit(1);
}
// When the string is empty
if (strlen(numPlate) == 0) {
return "No";
}
// Return the answer after validating the number plate
if (!regexec(&patt, numPlate, 0, NULL, 0)) {
return "Yes";
} else {
return "No";
}
}
int main() {
char num1[] = "GJ 03 AY 1097";
printf("Is %s Valid? - %s\n", num1, checkForNumberPlate(num1));
char num2[] = "TN 1A3 PZ 1287";
printf("Is %s Valid? - %s\n", num2, checkForNumberPlate(num2));
return 0;
}
输出
Is GJ 03 AY 1097 Valid? - Yes Is TN 1A3 PZ 1287 Valid? - No
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string checkForNumberPlate(string numPlate) {
// Defining the regular expression
const regex patt("^[A-Z]{2}[ -]?[0-9]{2}[ -]?[A-Z]{1,2}[ -]?[0-9]{4}$");
// When the string is empty
if (numPlate.empty()) {
return "No";
}
// Return the answer after validating the number plate
if (regex_match(numPlate, patt)) {
return "Yes";
} else {
return "No";
}
}
int main() {
string num1 = "GJ 03 AY 1097";
cout << "Is " << num1 << " Valid? - " << checkForNumberPlate(num1) << endl;
string num2 = "TN 1A3 PZ 1287";
cout << "Is " << num2 << " Valid? - " << checkForNumberPlate(num2) << endl;
return 0;
}
输出
Is GJ 03 AY 1097 Valid? - Yes Is TN 1A3 PZ 1287 Valid? - No
import java.util.regex.*;
public class NumberPlateValidator {
// Function to check if a number plate is valid
public static String checkForNumberPlate(String numPlate) {
// Defining the regular expression
String patt = "^[A-Z]{2}[ -]?[0-9]{2}[ -]?[A-Z]{1,2}[ -]?[0-9]{4}$";
Pattern pattern = Pattern.compile(patt);
// When the string is empty
if (numPlate.isEmpty()) {
return "No";
}
// Return the answer after validating the number plate
Matcher matcher = pattern.matcher(numPlate);
if (matcher.matches()) {
return "Yes";
} else {
return "No";
}
}
public static void main(String[] args) {
String num1 = "GJ 03 AY 1097";
System.out.println("Is " + num1 + " Valid? - " + checkForNumberPlate(num1));
String num2 = "TN 1A3 PZ 1287";
System.out.println("Is " + num2 + " Valid? - " + checkForNumberPlate(num2));
}
}
输出
Is GJ 03 AY 1097 Valid? - Yes Is TN 1A3 PZ 1287 Valid? - No
import re
# Function to check if a number plate is valid
def checkForNumberPlate(numPlate):
# Defining the regular expression
patt = "^[A-Z]{2}[ -]?[0-9]{2}[ -]?[A-Z]{1,2}[ -]?[0-9]{4}$"
# When the string is empty
if len(numPlate) == 0:
return "No"
# Return the answer after validating the number plate
if re.match(patt, numPlate):
return "Yes"
else:
return "No"
if __name__ == "__main__":
num1 = "GJ 03 AY 1097"
print(f"Is {num1} Valid? - {checkForNumberPlate(num1)}")
num2 = "TN 1A3 PZ 1287"
print(f"Is {num2} Valid? - {checkForNumberPlate(num2)}")
输出
Is GJ 03 AY 1097 Valid? - Yes Is TN 1A3 PZ 1287 Valid? - No
时间复杂度 − O(N) 以匹配模式。
空间复杂度 − O(1),因为我们没有使用任何额外的空间。
程序员可以尝试创建另一个正则表达式来验证印度车辆牌照。例如,它不应该允许在正则表达式之间使用空格或连字符。此外,regex_search() 方法可以使用正则表达式验证字符串。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP