International Tracking of Exports validation using Regular Expression
Given some International Tracking Of Exports data, the task is to check if they are valid or not using regular expressions. Rules for the valid International Tracking Of Exports are :
- It is an alphanumeric string containing UpperCase letters and digits.
- It either follows the pattern 2 Letters(A-Z)+ 9 Digits+GB Or 12 Digits(0-9).
- It should not contain whitespaces and other special characters.
- If it starts with an Upper case letter then it should end with “GB”
- Its length may vary from 12 to 13.
- If it starts with an alphabet letter then its length should be equal to 13 e.g.AA123456789GB
- If it starts with a digit, Its length should be equal to 12 e.g.123456789012
Examples:
Input: str = ”AA123456789GB″
Output: TrueInput: str = ”12345678901″
Output: False
Explanation: As it starts with a digit so its length should be equal to 12
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex = “^([0 – 9]{12}|[A – Z]{2}[0 – 9]{9}GB)$“Where,
- ^ : Start of the string
- [0-9]{12}: This pattern will match 12 of the preceding items if they are digits(0-9)
- [A-Z]{2}: This pattern will match two of the preceding items if they are Uppercase Alphabet letters
- [0-9]{9}: This pattern will allow 9 of the preceding tokens if they are digits
- GB: It will allow exactly “GB”
- $: End of the string
Follow the below steps to implement the idea:
- Create a regex expression for International Tracking Of Exports.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Tracking id is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the implementation of the above approach.
C++
// C++ program to validate the // International Tracking Of Exports // using Regular Expression #include <bits/stdc++.h> #include <regex> using namespace std; // Function to validate the // International Tracking Of Exports string isValid_Exports(string str) { // Regex to check valid // International Tracking Of Exports const regex pattern( "^([0-9]{12}|[A-Z]{2}[0-9]{9}GB)$" ); // If the str // is empty return false if (str.empty()) { return "false" ; } // Return true if the str // matched the ReGex if (regex_match(str, pattern)) { return "true" ; } else { return "false" ; } } // Driver Code int main() { // Test Case 1: string str1 = "AA123456789GB" ; cout << isValid_Exports(str1) << endl; // Test Case 2: string str2 = "123456789012" ; cout << isValid_Exports(str2) << endl; // Test Case 3: string str3 = "12345678901" ; cout << isValid_Exports(str3) << endl; // Test Case 4: string str4 = "AA123456789" ; cout << isValid_Exports(str4) << endl; // Test Case 5: string str5 = "AA12345678GB" ; cout << isValid_Exports(str5) << endl; return 0; } |
Java
// Java program to validate the // International Tracking Of Exports // using Regular Expression import java.util.regex.*; class GFG { // Function to validate the // International Tracking Of Exports public static boolean isValid_Exports(String str) { // Regex to check valid International // Tracking Of Exports String regex = "^([0-9]{12}|[A-Z]{2}[0-9]{9}GB)$" ; // Compile the ReGex Pattern p = Pattern.compile(regex); // If the str // is empty return false if (str == null ) { return false ; } // Pattern class contains matcher() // method to find matching between // given str using regular expression. Matcher m = p.matcher(str); // Return if the str // matched the ReGex return m.matches(); } // Driver Code. public static void main(String args[]) { // Test Case 1: String str1 = "AA123456789GB" ; System.out.println(isValid_Exports(str1)); // Test Case 2: String str2 = "123456789012" ; System.out.println(isValid_Exports(str2)); // Test Case 3: String str3 = "12345678901" ; System.out.println(isValid_Exports(str3)); // Test Case 4: String str4 = "AA123456789" ; System.out.println(isValid_Exports(str4)); // Test Case 5: String str5 = "AA12345678GB" ; System.out.println(isValid_Exports(str5)); } } |
Python3
# Python3 program to validate # International Tracking Of Exports using Regex import re # Function to validate # International Tracking Of Exports def isValid_Exports( str ): # Regex to check valid International # Tracking Of Exports regex = "^([0-9]{12}|[A-Z]{2}[0-9]{9}GB)$" # Compile the ReGex p = re. compile (regex) # If the string is empty # return false if ( str = = None ): return "false" # Return if the string # matched the ReGex if (re.search(p, str )): return "true" else : return "false" # Driver code if __name__ = = '__main__' : # Test Case 1: str1 = "AA123456789GB" print (isValid_Exports(str1)) # Test Case 2: str2 = "123456789012" print (isValid_Exports(str2)) # Test Case 3: str3 = "12345678901" print (isValid_Exports(str3)) # Test Case 4: str4 = "AA123456789" print (isValid_Exports(str4)) # Test Case 5: str5 = "AA12345678GB" print (isValid_Exports(str5)) |
C#
// C# program to validate the // International Tracking Of Exports // using Regular Expressions using System; using System.Text.RegularExpressions; class GFG { // Main Method static void Main( string [] args) { // Input strings to Match // International Tracking Of Exports string [] str = { "AA123456789GB" , "123456789012" , "12345678901" , "AA123456789" , "AA12345678GB" }; foreach ( string s in str) { Console.WriteLine(isValid_Exports(s) ? "true" : "false" ); } Console.ReadKey(); } // method containing the regex public static bool isValid_Exports( string str) { string strRegex = @"^([0-9]{12}|[A-Z]{2}[0-9]{9}GB)$" ; Regex re = new Regex(strRegex); if (re.IsMatch(str)) return ( true ); else return ( false ); } } |
Javascript
// Javascript program to validate // International Tracking Of Exports //using Regular Expression // Function to validate the // International Tracking Of Exports function isValid_Exports(str) { // Regex to check valid // International Tracking Of Exports let regex = new RegExp(/^([0-9]{12}|[A-Z]{2}[0-9]{9}GB)$/); // if str // is empty return false if (str == null ) { return "false" ; } // Return true if the str // matched the ReGex if (regex.test(str) == true ) { return "true" ; } else { return "false" ; } } // Driver Code // Test Case 1: let str1 = "AA123456789GB" ; console.log(isValid_Exports(str1)); // Test Case 2: let str2 = "123456789012" ; console.log(isValid_Exports(str2)); // Test Case 3: let str3 = "12345678901" ; console.log(isValid_Exports(str3)); // Test Case 4: let str4 = "AA123456789" ; console.log(isValid_Exports(str4)); // Test Case 5: let str5 = "AA12345678GB" ; console.log(isValid_Exports(str5)); |
Output
true true false false false
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Please Login to comment...