Sunday, September 22, 2024
Google search engine
HomeData Modelling & AIExtracting PAN Number from GST Number Using Regular Expressions

Extracting PAN Number from GST Number Using Regular Expressions

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;
 
// Function to extract PAN Number
// from a given string
void extractPAN_Number(string str)
{
    // String Array that hold the
    // pattern of the PAN Number
    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;
        }
    }
}
 
// Driver Code
int main()
{
    // String containing in it
    string str = "22BOSPC9911H1Z5";
    cout << "Given String is:" << endl;
    cout << str << endl;
    extractPAN_Number(str);
    return 0;
}
// This Code is Contributed by Prasad Kandekar(prasad264)


Java




// Java code for the above approach
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class GFG {
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // String containing in it
        String str = "22BOSPC9911H1Z5";
        System.out.println("Given String is:\n" + str);
        System.out.println(
            "The PAN Number that above string contains:");
        extractPAN_Number(str);
    }
 
    // Function to extract PAN Number
    // from a given string
    static void extractPAN_Number(String str)
    {
 
        // String Array that hold the
        // pattern of the PAN Number
        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
 
# Function to extract PAN Number
# from a given string
def extract_pan_number(string):
   
      # String Array that hold the
    # pattern of the PAN Number
    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)
 
 
# Driver Code
 
# String containing in it
string = "22BOSPC9911H1Z5"
print("Given string is:")
print(string)
extract_pan_number(string)
 
# This Code is Contributed by Prasad Kandekar(prasad264)


C#




// C# code for the above approach
using System;
using System.Text.RegularExpressions;
public class GFG{
 
    // Function to extract PAN Number
    // from a given string
    static void extractPAN_Number(string str)
    {
        // String Array that hold the
        // pattern of the PAN Number
        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);
        }
         
    }
    // Driver Code
    static public void Main (){
 
        // String containing in it
        string str = "22BOSPC9911H1Z5";
        Console.WriteLine("Given String is:\n" + str);
        Console.WriteLine("The PAN Number that above string contains:");
        extractPAN_Number(str);
    }
}


Javascript




// Function to extract PAN Number
// from a given string
function extractPAN_Number(str)
{
 
    // String Array that hold the
    // pattern of the PAN Number
    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]);
        }
    }
}
 
// Driver Code
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:

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments