PAN Number Regex Explained

A PAN is 5 letters, 4 digits and 1 letter. Here is the regex that checks that format, explained piece by piece.

AI DevRuns in your browser — nothing uploaded

Flags

What it means

Looks like it matches an Indian PAN number.

  1. ^Start of the text
  2. [A-Z]{5}Exactly 5 × an uppercase letter (A–Z)
  3. [0-9]{4}Exactly 4 × a digit (0–9)
  4. [A-Z]An uppercase letter (A–Z)
  5. $End of the text

Test cases

Should match

  • BBYNR7586Z
  • JRRHN6149I
  • UOARE8888G
  • YIKRF5651G
  • ZBQLK9174Y

Should not match

  • BBYNR7586
  • BBYNR7586Z!
  • BYNR7586Z
  • BBYNRx586Z
  • 7BYNR7586Z

Common mistakes in PAN regexes

Most broken PAN checks come from one of these small slips:

  1. Dropping ^ and $. Without them, /[A-Z]{5}[0-9]{4}[A-Z]/.test() is true for “XXABCPE1234FYY”, because the pattern only has to appear somewhere in the string.
  2. Writing [A-z] instead of [A-Z]. The range A–z also covers six symbols that sit between Z and a in ASCII, including [, _ and the backtick.
  3. Using \w for letters. It also matches digits and the underscore, so “AB_1E1234F” would get through.
  4. Adding the i flag to accept lower case. It works, but you then store a mix of cases. Upper-case the input first and keep the pattern case-sensitive.
  5. Using re.match in Python. It only anchors the start, so “ABCPE1234FXYZ” passes. Use re.fullmatch, or add $ to the pattern.
  6. Expecting the regex to check the last letter. It’s a check character, but its formula isn’t published, so any letter passes.

This pattern checks the shape only. A stricter version replaces the 4th [A-Z] with [PCHFATBLJG], the letters that mark the holder type — see the PAN & Aadhaar Validator for that version with ready-made code.

The PAN regex, token by token

The middle column is what the explainer above says about each part. The last column is what that part stands for in a PAN.

TokenWhat it matchesIn a PAN
^Start of the textNothing may come before the PAN
[A-Z]{5}Exactly 5 × an uppercase letter (A–Z)Series (3 letters), holder type and name initial
[0-9]{4}Exactly 4 × a digit (0–9)Running number, 0001–9999
[A-Z]An uppercase letter (A–Z)Check letter
$End of the textNothing may come after the PAN

Using the PAN regex in JavaScript, Python and SQL

Checking one value, and finding PANs inside a block of text. In free text, `\b` word boundaries replace `^` and `$`.

JavaScript
const PAN_RE = /^[A-Z]{5}[0-9]{4}[A-Z]$/;
PAN_RE.test("ABCPE1234F"); // true

// Find every PAN in a block of text
const text = "Vendor PAN: AAACI4798L, owner PAN: ABCPE1234F";
text.match(/\b[A-Z]{5}[0-9]{4}[A-Z]\b/g); // ["AAACI4798L", "ABCPE1234F"]
Python
import re

PAN_RE = re.compile(r"[A-Z]{5}[0-9]{4}[A-Z]")
bool(PAN_RE.fullmatch("ABCPE1234F"))  # True

text = "Vendor PAN: AAACI4798L, owner PAN: ABCPE1234F"
re.findall(r"\b[A-Z]{5}[0-9]{4}[A-Z]\b", text)  # ['AAACI4798L', 'ABCPE1234F']
SQL — find rows with a badly formatted PAN
-- MySQL 8: 'c' makes the match case-sensitive
SELECT id, pan FROM vendors
WHERE NOT REGEXP_LIKE(pan, '^[A-Z]{5}[0-9]{4}[A-Z]$', 'c');

-- PostgreSQL: !~ means "does not match", case-sensitive
SELECT id, pan FROM vendors
WHERE pan !~ '^[A-Z]{5}[0-9]{4}[A-Z]$';

How it works

  1. 1

    Parses the pattern

    The regex is broken into its parts — characters, classes, groups, quantifiers, anchors and lookarounds.

  2. 2

    Explains each part

    Every piece is described in plain English, nested the same way as the pattern, so groups and alternatives stay readable.

  3. 3

    Generates test cases

    Random strings are generated from the pattern and checked against it, then mutated to find near-misses that fail.

  4. 4

    Recognises common formats

    If a pattern matches a known format — PAN, GSTIN, email, Indian mobile number, PIN code, IFSC — it says so.

Questions, answered

Exactly five of whatever comes just before it. [A-Z]{5} is five capital letters in a row — the same as writing [A-Z][A-Z][A-Z][A-Z][A-Z].

Popular uses