VOOZH about

URL: https://www.geeksforgeeks.org/dsa/international-tracking-of-exports-validation-using-regular-expression/

⇱ International Tracking of Exports validation using Regular Expression - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

International Tracking of Exports validation using Regular Expression

Last Updated : 23 Jul, 2025

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: True

Input: 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.


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:

Comment