## Validating Indian vehicle number plate using Regular Expression

Last Updated: 03-Dec-2023 10:17:57
600 Views

In this problem, we will validate the Indian vehicle number plate using the regular expression.

The regular expression is the search pattern created using the different characters, which we can use to match a particular pattern in the given string.

Problem statement - We have given a string representing the Indian vehicle number plate. We need to use the regular expression to validate the given string.

## Sample Example

Input: num1 = "GJ 03 AY 1097"
Output: Yes


Explanation - The given vehicle number plate is valid.

Input: num2 = "TN 1A3 PZ 1287"
Output: No


Explanation - The given number plate is invalid due to '1A3'.

Input: num2 = "DL 29 KJX 0001"
Output: No


Explanation - The given number plate is invalid due to 'KJX'.

The Example of a valid Indian vehicle number plate is GJ 03 AY 1097.

• The number plate contains 2 characters of state number.

• After that, it contains the 2 digits, representing the district number.

• Next, it contains 1 or 2 alphabetical characters.

• At last, it contains the 4 numeric digits.

Users can follow the below regular expression to validate the Indian vehicle number plate.

"^[A-Z]{2}[ -]?[0-9]{2}[ -]?[A-Z]{1,2}[ -]?[0-9]{4}$" Let's understand the regular expression. • ^ − It represents the starting of the number plate. • [A - Z]{2} − It should contain 2 alphabetical characters in the uppercase. • [ -]? − After that, it may contain space or a hyphen. • [0-9]{2}[ -]? − It should contain 2 digits representing the district number. • [A-Z]{1,2} − It should contain 1 or 2 alphabetical characters. • [0-9]{4}$ − At the end, it should contain 4 numeric digits.

### Algorithm

• Step 1 − Define the regular expression pattern named patt.

• Step 2 − If the string is empty, return 'No'.

• Step 3 − Use the regex_match() method to validate the number plate string with 'patt'.

• Step 4 − If the regex_match() method returns true, return 'yes' from the function. Else, return 'No' from the function.

### Example

Following are the programs to the above algorithm in various programming languages

#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; }  ### Output 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;
}


### Output

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)); } }  ### Output 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)}")


### Output

Is GJ 03 AY 1097 Valid? - Yes
Is TN 1A3 PZ 1287 Valid? - No


Time complexity − O(N) to match the pattern.

Space complexity − O(1), as we don't use any extra space.

Programmers may try to create another regular expression to validate the Indian vehicle number plate. For example, it should not allow space or hyphens between regular expressions. Also, the regex_search() method can validate the string using the regular expression.

##### Prathmesh Yelne Follow
CEO & Founder of Codegyan