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
Common mistakes in PAN regexes
Most broken PAN checks come from one of these small slips:
- 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. - 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. - Using
\wfor letters. It also matches digits and the underscore, so “AB_1E1234F” would get through. - Adding the
iflag 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. - Using
re.matchin Python. It only anchors the start, so “ABCPE1234FXYZ” passes. Usere.fullmatch, or add$to the pattern. - 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.
| Token | What it matches | In a PAN |
|---|---|---|
^ | Start of the text | Nothing 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 text | Nothing 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 `$`.
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"]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']-- 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
Parses the pattern
The regex is broken into its parts — characters, classes, groups, quantifiers, anchors and lookarounds.
- 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
Generates test cases
Random strings are generated from the pattern and checked against it, then mutated to find near-misses that fail.
- 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].
^ means the start of the text and $ the end, so the whole value must be a PAN and nothing else. Leave them out and a PAN with extra characters around it would pass.
No — [A-Z] only matches capital letters. Convert the input to upper case before testing rather than loosening the pattern.
No. A regex only checks the format. The last letter is a check character with an unpublished formula, and only the Income Tax Department can confirm that a PAN was issued.
Use \b[A-Z]{5}[0-9]{4}[A-Z]\b with a global search — findall in Python, match with the g flag in JavaScript. \b stops it matching inside longer codes, such as the PAN inside a GSTIN.
Popular uses
Related tools
- Prompt CompressorCut tokens from prompts and code before you send them to an LLM.AI Dev
- AI SQL Query GeneratorEnglish to SQL for Postgres, MySQL and MSSQL.AI Dev
- JSON to Pydantic / TSInstant Pydantic models, TypeScript types or dataclasses.AI Dev
- JSON to TOON ConverterShrink JSON for LLM prompts with TOON — and convert back.AI Dev