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

Given a string str, the task is to check whether the given string is 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:

1. It should be 15 characters long.
2. The first 2 characters should be a number.
3. The next 10 characters should be PAN number of the taxpayer.
4. The 13th character (entity code) should be a number from 1-9 or an alphabet.
5. The 14th character should be Z.
6. 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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

1. Get the String.
2. 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 a 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.
3. Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher().
4. Return true if the string matches with the given regular expression, else return false.

Below is the implementation of the above approach:

 `// 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)); ` `    ``} ` `} `

Output:

```true
false
false
false
false
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.