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++
#include <iostream>
#include <regex>
using namespace std;
bool isValidGSTNo(string str)
{
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 (str.empty())
{
return false ;
}
if (regex_match(str, pattern))
{
return true ;
}
else
{
return false ;
}
}
int main()
{
string str1 = "06BZAHM6385P6Z2" ;
cout << isValidGSTNo(str1) << endl;
string str2 = "06BZAF67" ;
cout << isValidGSTNo(str2) << endl;
string str3 = "AZBZAHM6385P6Z2" ;
cout << isValidGSTNo(str3) << endl;
string str4 = "06BZ63AHM85P6Z2" ;
cout << isValidGSTNo(str4) << endl;
string str5 = "06BZAHM6385P6F2" ;
cout << isValidGSTNo(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValidGSTNo(String str)
{
String regex = "^[0-9]{2}[A-Z]{5}[0-9]{4}"
+ "[A-Z]{1}[1-9A-Z]{1}"
+ "Z[0-9A-Z]{1}$" ;
Pattern p = Pattern.compile(regex);
if (str == null )
{
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "06BZAHM6385P6Z2" ;
System.out.println(isValidGSTNo(str1));
String str2 = "06BZAF67" ;
System.out.println(isValidGSTNo(str2));
String str3 = "AZBZAHM6385P6Z2" ;
System.out.println(isValidGSTNo(str3));
String str4 = "06BZ63AHM85P6Z2" ;
System.out.println(isValidGSTNo(str4));
String str5 = "06BZAHM6385P6F2" ;
System.out.println(isValidGSTNo(str5));
}
}
|
Python3
import re
def isValidMasterCardNo( str ):
regex = "^[0-9]{2}[A-Z]{5}[0-9]{4}" +
"[A-Z]{1}[1-9A-Z]{1}" +
"Z[0-9A-Z]{1}$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "06BZAHM6385P6Z2"
print (isValidMasterCardNo(str1))
str2 = "06BZAF67"
print (isValidMasterCardNo(str2))
str3 = "AZBZAHM6385P6Z2"
print (isValidMasterCardNo(str3))
str4 = "06BZ63AHM85P6Z2"
print (isValidMasterCardNo(str4))
str5 = "06BZAHM6385P6F2"
print (isValidMasterCardNo(str5))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main( string [] args)
{
string [] str={ "06BZAHM6385P6Z2" , "06BZAF67" , "AZBZAHM6385P6Z2" , "06BZ63AHM85P6Z2" , "06BZAHM6385P6F2" };
foreach ( string s in str) {
Console.WriteLine( isValidGSTNo(s) ? "true" : "false" );
}
Console.ReadKey(); }
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 );
}
}
|
Javascript
function isValidGSTNo(str) {
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}$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "06BZAHM6385P6Z2" ;
console.log(isValidGSTNo(str1));
let str2 = "06BZAF67" ;
console.log(isValidGSTNo(str2));
let str3 = "AZBZAHM6385P6Z2" ;
console.log(isValidGSTNo(str3));
let str4 = "06BZ63AHM85P6Z2" ;
console.log(isValidGSTNo(str4));
let str5 = "06BZAHM6385P6F2" ;
console.log(isValidGSTNo(str5));
|
Outputtrue
false
false
false
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)