Extracting PAN Number from GST Number Using Regular Expressions
Last Updated :
09 Mar, 2023
Given a string str in the form of a GST Number, the task is to extract the PAN Number from the given string.
General Format of a GST Number: “22AAAAA0000A1Z5″
- 22: State Code
- AAAAA0000A: Permanent Account Number (PAN)
- 1: Entity Number of the same PAN
- Z: Alphabet Z by default
- 5: Checksum digit
Examples:
Input: str=”23BOSPC9911R2Z5
Output: BOSPC9911R
Input: str = “22AAAAA0000A1Z5”
Output: AAAAA0000A
Approach: The problem can be solved based on the following idea:
General Format of PAN Number and GST Number:
- PAN_Number_Regex = “^[A-Z]{5}[0-9]{4}[A-Z]{1}$”
- GST_Number_Regex = “^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[0-9]{1}[A-Z]{1}[0-9]{1}$”
Create a regex pattern for PAN Number to validate the PAN number as written above:
Where,
- ^ : Start of the String .
- [A-Z]{5}: This pattern will match five of the preceding items if they all are matched in the range from A to Z..
- [0-9]{4}: This pattern will match four of the preceding items if they all are matched in the range from 0 to 9 .
- $: End of the String.
Follow the below steps to implement the idea:
- Create a regex expression to extract the PAN Number from the string.
- Use Pattern class to compile the regex formed.
- Use the matcher function to find.
- Below is the code implementation of the above-discussed approach:
C++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
void extractPAN_Number(string str)
{
string strPattern[] = { "[A-Z]{5}[0-9]{4}[A-Z]{1}" };
for ( int i = 0; i < 1; i++) {
regex pattern(strPattern[i]);
auto words_begin = sregex_iterator(
str.begin(), str.end(), pattern);
auto words_end = sregex_iterator();
cout << "The PAN Number that above string contains:"
<< endl;
for (sregex_iterator i = words_begin;
i != words_end; ++i) {
smatch match = *i;
cout << match.str() << endl;
}
}
}
int main()
{
string str = "22BOSPC9911H1Z5" ;
cout << "Given String is:" << endl;
cout << str << endl;
extractPAN_Number(str);
return 0;
}
|
Java
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GFG {
public static void main(String[] args)
{
String str = "22BOSPC9911H1Z5" ;
System.out.println( "Given String is:\n" + str);
System.out.println(
"The PAN Number that above string contains:" );
extractPAN_Number(str);
}
static void extractPAN_Number(String str)
{
String strPattern[]
= { "[A-Z]{5}[0-9]{4}[A-Z]{1}" };
for ( int i = 0 ; i < strPattern.length; i++) {
Pattern pattern
= Pattern.compile(strPattern[i]);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
}
|
Python3
import re
def extract_pan_number(string):
pattern = "[A-Z]{5}[0-9]{4}[A-Z]{1}"
result = re.findall(pattern, string)
print ( "The PAN Number that the string contains:" )
for pan in result:
print (pan)
string = "22BOSPC9911H1Z5"
print ( "Given string is:" )
print (string)
extract_pan_number(string)
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG{
static void extractPAN_Number( string str)
{
string sentence = "[A-Z]{5}[0-9]{4}[A-Z]{1}" ;
Match match = Regex.Match(str, sentence, RegexOptions.IgnoreCase);
if (match.Success) {
Console.WriteLine(match.Value);
}
}
static public void Main (){
string str = "22BOSPC9911H1Z5" ;
Console.WriteLine( "Given String is:\n" + str);
Console.WriteLine( "The PAN Number that above string contains:" );
extractPAN_Number(str);
}
}
|
Javascript
function extractPAN_Number(str)
{
const strPattern = [ "[A-Z]{5}[0-9]{4}[A-Z]{1}" ];
for (let i = 0; i < 1; i++) {
const pattern = new RegExp(strPattern[i]);
const words_begin = str.matchAll(pattern);
console.log( "The PAN Number that above string contains:" );
for (const match of words_begin) {
console.log(match[0]);
}
}
}
const str = "22BOSPC9911H1Z5" ;
console.log( "Given String is:" );
console.log(str);
extractPAN_Number(str);
|
Output
Given String is:
22BOSPC9911H1Z5
The PAN Number that above string contains:
BOSPC9911H
Time Complexity: O(n) where n is the length of the input string.
Space Complexity: O(n)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...