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
|
C++
|
Python3
|
C#
|
Javascript
|
PHP
|
true true false false false
Related Articles:
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
|
C++
|
Python3
|
C#
|
Javascript
|
PHP
|
true true false false false
Related Articles: