How to validate GST (Goods and Services Tax) number using Regular Expression
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.
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.
- 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:
C++
// 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) number bool 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 Code int 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 |
Java
// 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
# Python3 program to validate # GST (Goods and Services Tax) number # using regular expression import 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#
// C# program to validate the // GST (Goods and Services Tax) number //using Regular Expressions using System; using System.Text.RegularExpressions; class GFG { // Main Method static void Main( string [] args) { // Input strings to Match // GST (Goods and Services Tax) number string [] str={ "06BZAHM6385P6Z2" , "06BZAF67" , "AZBZAHM6385P6Z2" , "06BZ63AHM85P6Z2" , "06BZAHM6385P6F2" }; foreach ( string s in str) { Console.WriteLine( isValidGSTNo(s) ? "true" : "false" ); } Console.ReadKey(); } // method containing the regex public static bool isValidGSTNo( string str) { string strRegex = @"^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$" ; Regex re = new Regex(strRegex); if (re.IsMatch(str)) return ( true ); else return ( false ); } } // This code is contributed by Rahul Chauhan |
Javascript
// JavaScript program to validate the // GST (Goods and Services Tax) number // using Regular Expression // Function to validate the // GST Number function isValidGSTNo(str) { // Regex to check valid // GST CODE let regex = new RegExp(/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/); // GST CODE // is empty return false if (str == null ) { return "false" ; } // Return true if the GST_CODE // matched the ReGex if (regex.test(str) == true ) { return "true" ; } else { return "false" ; } } // Driver Code // Test Case 1: let str1 = "06BZAHM6385P6Z2" ; console.log(isValidGSTNo(str1)); // Test Case 2: let str2 = "06BZAF67" ; console.log(isValidGSTNo(str2)); // Test Case 3: let str3 = "AZBZAHM6385P6Z2" ; console.log(isValidGSTNo(str3)); // Test Case 4: let str4 = "06BZ63AHM85P6Z2" ; console.log(isValidGSTNo(str4)); // Test Case 5: let str5 = "06BZAHM6385P6F2" ; console.log(isValidGSTNo(str5)); // This code is contributed by Rahul Chauhan |
true false false false false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Please Login to comment...