Validating Indian automobile quantity plate utilizing Regualr Expression



Given a string str, the duty is to validate if this string represents an Indian automobile quantity.

A Quantity Plate consists of three elements :    

Common Indian Automobile Quantity Plate

The place,

UP: First two Characters denote the State Title. Mainly, Preliminary Characters of a State is used i.e. If the automobile is from Uttar Pradesh then,
UP Will denote the State Title.

50:Subsequent Two Numbers point out District’s Sequential Quantity.

BY 1998:Subsequent 6 (it varies from 5 to six) letters are Distinctive alphanumeric string for every automobile.


Enter: str = ”MH 05 S 9954″
Output: True
Rationalization: MH – State Title 
05 – District Sequential Quantity
S 9954 – Alphanumeric Distinctive Code

Enter: str = ”MH 05 DL 9023 ”
Output: True

Enter: str = ”123@3459″
Output: False
Rationalization: It has a singular character that’s in opposition to the property of the Automobile Quantity Plate code.

Enter: str =”9345268″
Output: False
Rationalization: As Incorporates Quantity Solely

Strategy: The issue could be solved primarily based on the next thought:

This downside could be handled with common expression. Regex will validate the entered knowledge and can present the precise format:

Create a regex sample to validate the quantity as written beneath:   
regex=”^[A-Z]{2}[ -]{0, 1}[0-9]{2}[ -]{0, 1}[A-Z]{1, 2}[ -]{0, 1}[0-9]{4}$” the place

^: Starting of the String
[A-Z]: Character Set, matches a personality within the vary “A” to “Z”
{2}: Quantifier, Match of the 2 previous objects
[ -]{0, 1} : Matches  one previous merchandise, It may be both area() or Hyphen(-)
[0-9]{2}: Matches two previous objects, Each the merchandise ought to be within the vary 0 to 9
[A-Z]{1, 2}: It should match between  1 to 2, Merchandise ought to be within the vary “A”  to “Z” 
$: finish of the String

Comply with the steps talked about beneath to implement the thought:

  • Create a regex as proven above.
  • Match the enter string with the regex sample.
  • If the string matches the sample then it’s a legitimate quantity. In any other case, it isn’t a legitimate quantity.

Under is the Code implementation of the above method: 



#embody <bits/stdc++.h>

#embody <regex>

utilizing namespace std;


string isValidVehicleNumberPlate(string NUMBERPLATE)




    const regex sample(

        "^[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, sample)) {

        return "true";


    else {

        return "false";




int important()



    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;




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}$";


        Sample p = Sample.compile(regex);




        if (NUMBERPLATE == null) {

            return false;







        Matcher m = p.matcher(NUMBERPLATE);




        return m.matches();




    public static void important(String args[])




        String str1 = "UP 50 BY 1998";




        String str2 = "MH-05-DL-9023";




        String str3 = "BNZAA2318JM";




        String str4 = "MH 05 S 9954";




        String str5 = "934517865";




        String str6 = "MH05DL9023";






Time Complexity: O(N) the place N is the size of the string
Auxiliary House: O(1)