Validating Indian vehicle number plate using Regular Expression
Last Updated :
06 Jul, 2023
Given a string str, the task is to validate if this string represents an Indian vehicle number.
A Number Plate consists of three parts :
General Indian Vehicle Number Plate
Where,
UP: First two Characters denote the State Name. Basically, Initial Characters of a State is used i.e. If the vehicle is from Uttar Pradesh then,
UP Will denote the State Name.
50:Next Two Numbers indicate District’s Sequential Number.
BY 1998:Next 6 (it varies from 5 to 6) letters are Unique alphanumeric string for each vehicle.
Examples:
Input: str = ”MH 05 S 9954?
Output: True
Explanation: MH – State Name
05 – District Sequential Number
S 9954 – Alphanumeric Unique Code
Input: str = ”MH 05 DL 9023 ”
Output: True
Input: str = ”123@3459?
Output: False
Explanation: It has a unique character that is against the property of the Vehicle Number Plate code.
Input: str =”9345268?
Output: False
Explanation: As Contains Number Only
Approach: The problem can be solved based on the following idea:
This problem can be dealt with regular expression. Regex will validate the entered data and will provide the exact format:
Create a regex pattern to validate the number as written below:
regex=”^[A-Z]{2}[\\ -]{0, 1}[0-9]{2}[\\ -]{0, 1}[A-Z]{1, 2}[\\ -]{0, 1}[0-9]{4}$” where
^: Beginning of the String
[A-Z]: Character Set, matches a character in the range “A” to “Z”
{2}: Quantifier, Match of the two preceding items
[\\ -]{0, 1} : Matches one preceding item, It can be either space(\\) or Hyphen(-)
[0-9]{2}: Matches two preceding items, Both the item should be in the range 0 to 9
[A-Z]{1, 2}: It will match between 1 to 2, Item should be in the range “A” to “Z”
$: end of the String
Follow the steps mentioned below to implement the idea:
- Create a regex as shown above.
- Match the input string with the regex pattern.
- If the string matches the pattern then it is a valid number. Otherwise, it is not a valid number.
Below is the Code implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string isValidVehicleNumberPlate(string NUMBERPLATE)
{
const regex pattern(
"^[A-Z]{2}[\\ -]{0,1}[0-9]{2}[\\ "
"-]{0,1}[A-Z]{1,2}[\\ -]{0,1}[0-9]{4}$" );
if (NUMBERPLATE.empty()) {
return "false" ;
}
if (regex_match(NUMBERPLATE, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "UP 50 BY 1998" ;
cout << isValidVehicleNumberPlate(str1) << endl;
string str2 = "MH-05-DL-9023" ;
cout << isValidVehicleNumberPlate(str2) << endl;
string str3 = "BNZAA2318JM" ;
cout << isValidVehicleNumberPlate(str3) << endl;
string str4 = "MH 05 S 9954" ;
cout << isValidVehicleNumberPlate(str4) << endl;
string str5 = "934517865" ;
cout << isValidVehicleNumberPlate(str5) << endl;
string str6 = "MH05DL9023" ;
cout << isValidVehicleNumberPlate(str6) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean
isValidVehicleNumberPlate(String NUMBERPLATE)
{
String regex
= "^[A-Z]{2}[\\ -]{0,1}[0-9]{2}[\\ -]{0,1}[A-Z]{1,2}[\\ -]{0,1}[0-9]{4}$" ;
Pattern p = Pattern.compile(regex);
if (NUMBERPLATE == null ) {
return false ;
}
Matcher m = p.matcher(NUMBERPLATE);
return m.matches();
}
public static void main(String args[])
{
String str1 = "UP 50 BY 1998" ;
System.out.println(isValidVehicleNumberPlate(str1));
String str2 = "MH-05-DL-9023" ;
System.out.println(isValidVehicleNumberPlate(str2));
String str3 = "BNZAA2318JM" ;
System.out.println(isValidVehicleNumberPlate(str3));
String str4 = "MH 05 S 9954" ;
System.out.println(isValidVehicleNumberPlate(str4));
String str5 = "934517865" ;
System.out.println(isValidVehicleNumberPlate(str5));
String str6 = "MH05DL9023" ;
System.out.println(isValidVehicleNumberPlate(str6));
}
}
|
Python3
import re
def isValidVehicleNumberPlate( str ):
regex = "^[A-Z]{2}[\\s-]{0,1}[0-9]{2}[\\s-]{0,1}[A-Z]{1,2}[\\s-]{0,1}[0-9]{4}$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "UP 50 BY 1998"
print (isValidVehicleNumberPlate(str1))
str2 = "MH-05-DL-9023"
print (isValidVehicleNumberPlate(str2))
str3 = "BNZAA2318JM"
print (isValidVehicleNumberPlate(str3))
str4 = "MH 05 S 9954"
print (isValidVehicleNumberPlate(str4))
str5 = "934517865"
print (isValidVehicleNumberPlate(str5))
str6 = "MH05DL9023"
print (isValidVehicleNumberPlate(str6))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
public static bool
isValidVehicleNumberPlate(String NUMBERPLATE)
{
string regex
= "^[A-Z]{2}[\\ -]{0,1}[0-9]{2}[\\ -]{0,1}[A-Z]{1,2}[\\ -]{0,1}[0-9]{4}$" ;
Regex p = new Regex(regex);
if (NUMBERPLATE == null ) {
return false ;
}
Match m = p.Match(NUMBERPLATE);
return m.Success;
}
public static void Main()
{
string str1 = "UP 50 BY 1998" ;
Console.WriteLine(isValidVehicleNumberPlate(str1));
string str2 = "MH-05-DL-9023" ;
Console.WriteLine(isValidVehicleNumberPlate(str2));
string str3 = "BNZAA2318JM" ;
Console.WriteLine(isValidVehicleNumberPlate(str3));
string str4 = "MH 05 S 9954" ;
Console.WriteLine(isValidVehicleNumberPlate(str4));
string str5 = "934517865" ;
Console.WriteLine(isValidVehicleNumberPlate(str5));
string str6 = "MH05DL9023" ;
Console.WriteLine(isValidVehicleNumberPlate(str6));
}
}
|
Javascript
function isValidVehicleNumberPlate(NUMBERPLATE) {
let regex = new RegExp(/^[A-Z]{2}[ -][0-9]{1,2}(?: [A-Z])?(?: [A-Z]*)? [0-9]{4}$/);
if (NUMBERPLATE == null ) {
return "false" ;
}
if (regex.test(NUMBERPLATE) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "UP 50 BY 1998" ;
console.log(isValidVehicleNumberPlate(str1));
let str2 = "MH 05 DL 9023" ;
console.log(isValidVehicleNumberPlate(str2));
let str3 = "BNZAA2318JM" ;
console.log(isValidVehicleNumberPlate(str3));
let str4 = "MH 05 S 9954" ;
console.log(isValidVehicleNumberPlate(str4));
let str5 = "934517865" ;
console.log(isValidVehicleNumberPlate(str5));
let str6 = "MH 05 DL 9023" ;
console.log(isValidVehicleNumberPlate(str6));
|
Output
true
true
false
true
false
true
Time Complexity: O(N) where N is the length of the string
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...