Friday, October 21, 2022
HomeWordPress DevelopmentValidating Indian car quantity plate utilizing Regualr Expression

Validating Indian car quantity plate utilizing Regualr Expression


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

A Quantity Plate consists of three elements :    

Basic Indian Car Quantity Plate

The place,

UP: First two Characters denote the State Identify. Principally, Preliminary Characters of a State is used i.e. If the car is from Uttar Pradesh then,
UP Will denote the State Identify.

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 car.

Examples:

Enter: str = ”MH 05 S 9954″
Output: True
Rationalization: MH – State Identify 
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 novel character that’s towards the property of the Car Quantity Plate code.

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

Strategy: The issue may be solved based mostly on the next concept:

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

Create a regex sample to validate the quantity as written under:   
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 house() or Hyphen(-)
[0-9]{2}: Matches two previous objects, Each the merchandise needs to be within the vary 0 to 9
[A-Z]{1, 2}: It’ll match between  1 to 2, Merchandise needs to be within the vary “A”  to “Z” 
$: finish of the String

Observe the steps talked about under 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’s not a legitimate quantity.

Under is the Code implementation of the above strategy: 

C++

  

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

}

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

        

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

        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));

    }

}

Output

true
true
false
true
false
true

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments