REGEXVAULTv2.0
Identity & PII/National Identity Numbers
Verified Safe

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

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

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 Go. 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 Go project — whether you're validating in a Gin handler, a gRPC service, or a command-line tool.

Go Implementation

Go
// Brazilian CNPJ (Cadastro Nacional da Pessoa Jurídica)
// ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers

package validation

import "regexp"

var brazilianCnpjCadastroNacionalDaPessoaJurdicaRe = regexp.MustCompile(`^(\d{2})\.?(\d{3})\.?(\d{3})/?([0-9]{4})-?(\d{2})$`)

func ValidateBrazilianCnpjCadastroNacionalDaPessoaJurdica(s string) bool {
    return brazilianCnpjCadastroNacionalDaPessoaJurdicaRe.MatchString(s)
}

// Example
// fmt.Println(ValidateBrazilianCnpjCadastroNacionalDaPessoaJurdica("11.222.333/0001-81")) // 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 carries a ReDoS-safe certification. That matters for Go developers because Go's RE2 engine is inherently safe from catastrophic backtracking, but this pattern has been additionally verified for correctness. 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. Free submissions join the queue. Priority review available for $15.

Submit a Pattern