REGEXVAULT
Identity & PII/National Identity Numbers
ReDoS Checked

Brazilian CNPJ (Cadastro Nacional da Pessoa Jurídica) Regex for PHP

/^(\d{2})\.?(\d{3})\.?(\d{3})/?([0-9]{4})-?(\d{2})$/

All five implementations are free. Switch engines without signing in.

What this pattern does

This page provides a well-structured, multi-part regular expression for matching brazilian cnpj (cadastro nacional da pessoa jurídica), ported and verified for PHP. Identity and credential patterns need both correctness and safety, since they're frequent targets for adversarial input. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.

Php Implementation

Php
<?php
// Brazilian CNPJ (Cadastro Nacional da Pessoa Jurídica)
// ReDoS-checked | RegexVault — Identity & PII > National Identity Numbers

define('BRAZILIAN_CNPJ_CADASTRO_NACIONAL_DA_PESSOA_JURDICA_PATTERN', '/^(\d{2})\.?(\d{3})\.?(\d{3})\/?([0-9]{4})-?(\d{2})$/');

function validate_brazilian_cnpj_cadastro_nacional_da_pessoa_jurdica(string $input): bool {
    return (bool) preg_match(BRAZILIAN_CNPJ_CADASTRO_NACIONAL_DA_PESSOA_JURDICA_PATTERN, $input);
}

// Example
var_dump(validate_brazilian_cnpj_cadastro_nacional_da_pessoa_jurdica("11.222.333/0001-81")); // bool(true)

Test Cases

Matches (Valid)
Rejects (Invalid)
11.222.333/0001-8111.222.333/001-81
1122233300018111222333000181234
00.000.000/0001-9111.222.333/0001-8

When to use this pattern

This pattern is drawn from the Identity & PII > National Identity Numbers category and has been checked for ReDoS risk. That matters for PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

CNPJ branches allow one company to have many CNPJs. 11.222.333/0001-81 and 11.222.333/0002-62 are the same company, different branches. Store the full 14-digit CNPJ to distinguish branches.

Technical Notes

CNPJ format: 14 digits (8-digit base + 4-digit branch + 2 check digits). Branch 0001 is the main establishment; branches 0002+ are subsidiaries. All-same-digit numbers are invalid. Check digits use weighted sums similar to CPF but different weights.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Submissions join the review queue after automated validation.

Submit a Pattern