How to validate GST (Goods and Services Tax) number using Regular Expression

27 Jan, 2021
Given string str, the task is to check whether the given string is a valid GST (Goods and Services Tax) number or not using Regular Expression. The valid GST (Goods and Services Tax) number must satisfy the following conditions: 
It should be 15 characters long.The first 2 characters should be a number.The next 10 characters should be the PAN number of the taxpayer.The 13th character (entity code) should be a number from 1-9 or an alphabet.The 14th character should be Z.The 15th character should be an alphabet or a number.
It should be 15 characters long.
The first 2 characters should be a number.
The next 10 characters should be the PAN number of the taxpayer.
The 13th character (entity code) should be a number from 1-9 or an alphabet.
The 14th character should be Z.
The 15th character should be an alphabet or a number.
Examples: 
Input: str = “06BZAHM6385P6Z2”; Output: true Explanation: The given string satisfies all the above mentioned conditions. Therefore, it is a valid GST (Goods and Services Tax) number.
Input: str = “06BZAF67”; Output: false Explanation: The given string is not 15 characters long. Therefore, it is not a valid GST (Goods and Services Tax) number.
Input: str = “AZBZAHM6385P6Z2”; Output: false Explanation: The given string starts with alphabet. Therefore, it is not a valid GST (Goods and Services Tax) number. 
Approach: The idea is to use the concept of Regular Expression to solve this problem. The following steps can be followed to compute the answer: 
Get the String.
Create a regular expression to check valid GST (Goods and Services Tax) number as mentioned below:
regex = “^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$”; 
Where: ^ represents the starting of the string.[0-9]{2} represents the first two characters should be a number.[A-Z]{5} represents the next five characters should be any upper case alphabets.[0-9]{4} represents the next four characters should be any number.[A-Z]{1} represents the next character should be any upper case alphabet.[1-9A-Z]{1} represents the 13th character should be a number from 1-9 or an alphabet.Z represents the 14th character should be Z.[0-9A-Z]{1} represents the 15th character should be an alphabet or a number.$ represents the ending of the string.
^ represents the starting of the string.
[0-9]{2} represents the first two characters should be a number.
[A-Z]{5} represents the next five characters should be any upper case alphabets.
[0-9]{4} represents the next four characters should be any number.
[A-Z]{1} represents the next character should be any upper case alphabet.
[1-9A-Z]{1} represents the 13th character should be a number from 1-9 or an alphabet.
Z represents the 14th character should be Z.
[0-9A-Z]{1} represents the 15th character should be an alphabet or a number.
$ represents the ending of the string.
Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher().
Return true if the string matches with the given regular expression, else return false.
Below is the implementation of the above approach:
Java
Python3
C++
// Java program to validate the// GST (Goods and Services Tax) number// using regular expression. import java.util.regex.*; class GFG {     // Function to validate    // GST (Goods and Services Tax) number.    public static boolean isValidGSTNo(String str)    {        // Regex to check valid        // GST (Goods and Services Tax) number        String regex = "^[0-9]{2}[A-Z]{5}[0-9]{4}"                       + "[A-Z]{1}[1-9A-Z]{1}"                       + "Z[0-9A-Z]{1}$";         // Compile the ReGex        Pattern p = Pattern.compile(regex);         // If the string is empty        // return false        if (str == null)        {            return false;        }         // Pattern class contains matcher()        // method to find the matching        // between the given string        // and the regular expression.        Matcher m = p.matcher(str);         // Return if the string        // matched the ReGex        return m.matches();    }     // Driver Code.    public static void main(String args[])    {         // Test Case 1:        String str1 = "06BZAHM6385P6Z2";        System.out.println(isValidGSTNo(str1));         // Test Case 2:        String str2 = "06BZAF67";        System.out.println(isValidGSTNo(str2));         // Test Case 3:        String str3 = "AZBZAHM6385P6Z2";        System.out.println(isValidGSTNo(str3));         // Test Case 4:        String str4 = "06BZ63AHM85P6Z2";        System.out.println(isValidGSTNo(str4));         // Test Case 5:        String str5 = "06BZAHM6385P6F2";        System.out.println(isValidGSTNo(str5));    }}
# Python3 program to validate# GST (Goods and Services Tax) number # using regular expressionimport re # Function to validate GST# (Goods and Services Tax) number.def isValidMasterCardNo(str):     # Regex to check valid    # GST (Goods and Services Tax) number    regex = "^[0-9]{2}[A-Z]{5}[0-9]{4}" +            "[A-Z]{1}[1-9A-Z]{1}" +            "Z[0-9A-Z]{1}$"         # 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 = "06BZAHM6385P6Z2"print(isValidMasterCardNo(str1)) # Test Case 2:str2 = "06BZAF67"print(isValidMasterCardNo(str2)) # Test Case 3:str3 = "AZBZAHM6385P6Z2"print(isValidMasterCardNo(str3)) # Test Case 4:str4 = "06BZ63AHM85P6Z2"print(isValidMasterCardNo(str4)) # Test Case 5:str5 = "06BZAHM6385P6F2"print(isValidMasterCardNo(str5)) # This code is contributed by avanitrachhadiya2155
// C++ program to validate the// GST (Goods and Services Tax) number// using Regular Expression#include <iostream>#include <regex>using namespace std; // Function to validate the GST// (Goods and Services Tax) numberbool isValidGSTNo(string str){     // Regex to check valid GST    // (Goods and Services Tax) number    const regex pattern("^[0-9]{2}[A-Z]{5}"                        "[0-9]{4}[A-Z]{1}["                        "1-9A-Z]{1}Z[0-9A-Z]{1}$");     // If the GST (Goods and Services Tax)    // number is empty return false    if (str.empty())    {        return false;    }     // Return true if the GST number    // matched the ReGex    if (regex_match(str, pattern))    {        return true;    }    else    {        return false;    }} // Driver Codeint main(){    // Test Case 1:    string str1 = "06BZAHM6385P6Z2";    cout << isValidGSTNo(str1) << endl;     // Test Case 2:    string str2 = "06BZAF67";    cout << isValidGSTNo(str2) << endl;     // Test Case 3:    string str3 = "AZBZAHM6385P6Z2";    cout << isValidGSTNo(str3) << endl;     // Test Case 4:    string str4 = "06BZ63AHM85P6Z2";    cout << isValidGSTNo(str4) << endl;     // Test Case 5:    string str5 = "06BZAHM6385P6F2";    cout << isValidGSTNo(str5) << endl;     return 0;} // This code is contributed by yuvraj_chandra
true
false
false
false
false

avanitrachhadiya2155
yuvraj_chandra
CPP-regex
java-regular-expression
regular-expression
Pattern Searching
Strings
Strings
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.