给定字符串str ,任务是使用正则表达式检查给定的字符串是否为有效的GST(商品及服务税)号码。
有效的GST(商品及服务税)编号必须满足以下条件:
- 它应该是15个字符长。
- 前2个字符应为数字。
- 接下来的10个字符应为纳税人的PAN号码。
- 第13个字符(实体代码)应为1到9之间的数字或字母。
- 第14个字符应为Z。
- 第15个字符应为字母或数字。
例子:
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.
方法:想法是使用正则表达式的概念来解决此问题。可以按照以下步骤计算答案:
- 获取字符串。
- 创建一个正则表达式以检查有效的GST(商品和服务税)编号,如下所述:
regex = “^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$”;
- 在哪里:
- ^表示字符串的开头。
- [0-9] {2}表示前两个字符应为数字。
- [AZ] {5}表示接下来的五个字符应为任何大写字母。
- [0-9] {4}表示接下来的四个字符应为任意数字。
- [AZ] {1}表示下一个字符应该是任何大写字母。
- [1-9A-Z] {1}表示第13个字符应该是1-9中的数字或字母。
- Z表示第14个字符应为Z。
- [0-9A-Z] {1}表示第15个字符应为字母或数字。
- $表示字符串的结尾。
- 将给定的字符串与正则表达式匹配。在Java,这可以通过使用Pattern.matcher()来完成。
- 如果字符串与给定的正则表达式匹配,则返回true;否则返回false。
下面是上述方法的实现:
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 Expression
#include
#include
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
true
false
false
false
false