Regular Expression to Validate PAN Card Number in Python

Regular Expression to Validate PAN Card Number in Python

Problem Statement: Write a regular expression to validate PAN card number in Python programming.

M Stark works in the sales department. He has a paragraph P of words that contain some PAN card numbers.

He wants to count the number of distinct valid PAN card numbers in P.

A valid PAN card number is a 10-length alphanumeric word of the form “AAAAA1111A” (without quotes) where

  • A denotes any letter of the English uppercase alphabet.
  • 1 denotes any digit in 0, 1, 2, 3,..., 9
  • The fourth character can be only from the set {'A','B','C','F','G','H','L','J','P','T','K'}
  • The fifth character must be the same as the first character.

Example

MKOFM5336A is a valid PAN card number.

As checking manually will take a lot of time, so write a script for this task.

This challenge was asked in the Bright Money online coding test.

Algorithm

  • Write a regular expression (RegEx) for the PAN card matching pattern based on the rules mentioned in the problem.
    (Writing correct Python RegEx is an important and deciding factor for solving this problem.)
  • Use findall() method from re module to get the list of all the valid PAN numbers.
  • There can be some duplicate PAN card numbers, so use set() method to remove all duplicated PAN card numbers.

Python RegEx to find valid PAN Card Numbers

Let’s use the findall() method from re module.

Code

import re
 
def get_valid_pan_number(par):
    re_exp = r"[A-Z]{3}[ABCFGHLJPTK]{1}[A-Z]{1}[0-9]{4}[A-Z]{1}"
    return re.findall(re_exp, par)

par = "AABZA1111A AABAA1111AxMKOFM5336A"
print(set(get_valid_pan_number(par)))

Output

{'MKOFM5336A', 'AABAA1111A'}

Sometimes you may be asked to count the valid PAN card numbers.

Python RegEx Count Valid PAN card numbers

You can use len() method to calculate the number of valid PAN card numbers.

Code

import re
 
def get_valid_pan_number(par):
    re_exp = r"[A-Z]{3}[ABCFGHLJPTK]{1}[A-Z]{1}[0-9]{4}[A-Z]{1}"
    return re.findall(re_exp, par)

par = "AABZA1111A AABAA1111AxMKOFM5336A"
print(len(set(get_valid_pan_number(par))))

Output

2

Similar Problem on Regular Expression

Writing regular expressions makes your job very easy for pattern matching. If you want to solve this kind of problem manually (without using the RegEx module), it will be complex and cumbersome.

You can use any other programming language of your choice to solve this problem.

This is all about writing a program for a regular expression to validate PAN card number in Python. Do you find it useful? Let me know in the comment. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *