Regular Expressions to validate International Tracking of Imports
Given some International Tracking of Imports, the task is to check if they are valid or not using regular expressions. Rules for the valid International Tracking of Imports are:
- It is an alphanumeric string i.e., It contains UpperCase alphabet letters (A-Z) and digits(0-9).
- It does not contain whitespaces and other special characters.
- Its length should be equal to 13.
- It can follow below one of the written formats:
- 2 Letters(A-Z)+ 9 Digits(0-9)+2 letters i.e. [A-Z]{2}[0-9]{9}[A-Z]{2}
- 13 Digits(0-9)
Examples:
Input: str = ”AA123456789AA″
Output: trueInput: str = ”12345678901″
Output: False
Explanation: As it starts with a digit so its length should be equal to 13.
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex = “^([A-Z]{2}[0-9]{9}[A-Z]{2}| [0-9]{13})$“Where,
- ^: Start of the string
- [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.
- $: End of the string.
Follow the below steps to implement the idea:
- Create a regex expression for International Tracking of Imports.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the International Tracking of Imports Number is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the code implementation of the above-discussed approach:
Java
// Java program to validate the // International Tracking Of Imports // using Regular Expression import java.util.regex.*; class GFG { // Function to validate the // International Tracking Of Imports public static boolean isValid_Imports(String str) { // Regex to check valid International // Tracking Of Imports String regex = "^([A-Z]{2}[0-9]{9}[A-Z]{2}|[0-9]{13})$" ; // 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 = "AA123456789AA" ; System.out.println(isValid_Imports(str1)); // Test Case 2: String str2 = "RR123456785AB" ; System.out.println(isValid_Imports(str2)); // Test Case 3: String str3 = "12345678901" ; System.out.println(isValid_Imports(str3)); // Test Case 4: String str4 = "AA123456789" ; System.out.println(isValid_Imports(str4)); // Test Case 5: String str5 = "AA12345678AB" ; System.out.println(isValid_Imports(str5)); } } |
C++
// C++ program to validate the // International Tracking Of Imports // using Regular Expression #include <iostream> #include <regex> using namespace std; // Function to validate the // International Tracking Of Imports bool isValid_Imports(string str) { // Regex to check valid // International Tracking Of Imports const regex pattern( "^([A-Z]{2}[0-9]{9}[A-Z]{2}|[0-9]{13})$" ); // 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 = "AA123456789AA" ; cout << isValid_Imports(str1) << endl; // Test Case 2: string str2 = "RR123456785AB" ; cout << isValid_Imports(str2) << endl; // Test Case 3: string str3 = "12345678901" ; cout << isValid_Imports(str3) << endl; // Test Case 4: string str4 = "AA123456789" ; cout << isValid_Imports(str4) << endl; // Test Case 5: string str5 = "AA12345678AB" ; cout << isValid_Imports(str5) << endl; return 0; } |
Python3
# Python3 program to validate # International Tracking Of Imports using # Regular Expression import re # Function to validate # International Tracking Of Imports def isValid_Imports( str ): # Regex to check valid International # Tracking Of Imports regex = "^([A-Z]{2}[0-9]{9}[A-Z]{2}|[0-9]{13})$" # 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 # Test Case 1: str1 = "AA123456789AA" print (isValid_Imports(str1)) # Test Case 2: str2 = "RR123456785AB" print (isValid_Imports(str2)) # Test Case 3: str3 = "12345678901" print (isValid_Imports(str3)) # Test Case 4: str4 = "AA123456789" print (isValid_Imports(str4)) # Test Case 5: str5 = "AA12345678AB" print (isValid_Imports(str5)) |
C#
// C# program to validate the // International Tracking Of Imports // 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 Imports string [] str = { "AA123456789AA" , "RR123456785AB" , "12345678901" , "AA123456789" , "AA12345678AB" }; foreach ( string s in str) { Console.WriteLine(isValid_Imports(s) ? "true" : "false" ); } Console.ReadKey(); } // method containing the regex public static bool isValid_Imports( string str) { string strRegex = @"^([A-Z]{2}[0-9]{9}[A-Z]{2}|[0-9]{13})$" ; Regex re = new Regex(strRegex); if (re.IsMatch(str)) return ( true ); else return ( false ); } } |
Javascript
// Javascript program to validate // International Tracking Of Imports //using Regular Expression // Function to validate the // International Tracking Of Imports function isValid_Imports(str) { // Regex to check valid // International Tracking Of Imports let regex = new RegExp(/^([A-Z]{2}[0-9]{9}[A-Z]{2}|[0-9]{13})$/); // 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 = "AA123456789AA" ; console.log(isValid_Imports(str1)); // Test Case 2: let str2 = "RR123456785AB" ; console.log(isValid_Imports(str2)); // Test Case 3: let str3 = "12345678901" ; console.log(isValid_Imports(str3)); // Test Case 4: let str4 = "AA123456789" ; console.log(isValid_Imports(str4)); // Test Case 5: let str5 = "AA12345678AB" ; console.log(isValid_Imports(str5)); |
PHP
<?php // PHP program to validate International Tracking Of Imports // Function to validate International Tracking Of Imports using regular expression function isValid_Imports( $str ){ if (preg_match( '/^([A-Z]{2}[0-9]{9}[A-Z]{2}|[0-9]{13})$/' , $str )) { echo "true\n" ; } else { echo "false\n" ; } } isValid_Imports( "AA123456789AA" ); isValid_Imports( "RR123456785AB" ); isValid_Imports( "12345678901" ); isValid_Imports( "AA123456789" ); isValid_Imports( "AA12345678AB" ); ?> |
true true false false false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Please Login to comment...