给定字符串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
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live