Common regex library

Ready-made regular expressions: email, Brazilian and international phone, CPF, CNPJ, postal/ZIP, Pix, cards, SSN, IPv4, IPv6, YouTube, HTML, and more. Test in the browser and copy the pattern or snippet for PHP, JavaScript, TypeScript, Python, Go, Java, and C#.

{{ regexPresetExampleText() }}

Flags alteram o comportamento do regex. Ex.: i ignora maiúsculas/minúsculas, g encontra todas as ocorrências e m trata múltiplas linhas.

Script pronto por linguagem

Gere o padrão atual com sintaxe e escapes adequados para colar no seu projeto.

{{ regexLibrary.message }}

Tutorial rápido de expressões regulares

Regex é um jeito compacto de descrever padrões no texto: validar e-mails, extrair datas, limpar HTML e muito mais. Abaixo estão os blocos mais usados em JavaScript (e na maioria dos dialetos modernos).

Âncoras e limites

  • ^ início da string (ou da linha, com a flag m).
  • $ fim da string (ou da linha, com m).
  • \b limite de palavra (entre “caractere de palavra” e não palavra).

Classes e coringas

  • [abc] um dos caracteres listados.
  • [a-z] intervalo (aqui, letras minúsculas ASCII).
  • . quase qualquer caractere (exceto quebra de linha, salvo com flag s).

Atalhos com \

  • \d dígito (09).
  • \w caractere de “palavra” (letras, dígitos, _).
  • \s espaço em branco (espaço, tab, quebra de linha, etc.).
  • \. ponto literal (escapa o meta .).

Quantificadores

  • + uma ou mais vezes o elemento anterior.
  • * zero ou mais vezes.
  • ? zero ou uma vez (também torna outras formas preguiçosas com ? após um quantifier).
  • {3,6} entre 3 e 6 repetições.

Grupos e alternância

  • (...) grupo capturado (memoriza o trecho para referência ou substituição).
  • (?:...) grupo sem captura (só agrupa, não cria índice extra).
  • | “OU”: casa a expressão da esquerda ou da direita.

Olhar para frente (avançado)

  • (?=...) positive lookahead: exige que o padrão apareça logo depois, sem consumir.
  • (?!...) negative lookahead: falha se o padrão aparecer adiante.

Exemplo colorido: e-mail simples

Cada cor indica um tipo de construção. Compare com o texto do regex acima na ferramenta ao escolher o preset E-mail.

^[^\s@]+@[^\s@]+\.[^\s@]+$
  • Âncoras (^ e $)
  • Classe negada [^...] (aqui: tudo exceto espaço e @)
  • Texto literal (@)
  • Escape (\. força um ponto real, não o coringa)

Flags comuns nesta página

  • i ignora maiúsculas e minúsculas.
  • g busca todas as ocorrências (global).
  • m ^ e $ passam a valer por linha.

Overview

Stephen Kleene formalized regular expressions in 1951 in a seminal paper on finite automata and neural networks — at the time, the goal was to mathematically describe how electrical circuits and neurons could recognize patterns. The name regular expression comes from his notation for regular languages, the simplest type in the Chomsky hierarchy. The computational implementation began with Ken Thompson, the creator of Unix and cron: in 1968 he implemented regex in the QED editor at Bell Labs, then in the `ed` editor, and in 1973 `grep` appeared — whose name comes from the `ed` command `g/re/p`, meaning global, regular expression, print. It is impossible to use a Unix command line without encountering grep, and it remains one of the most-used tools by anyone who works with text and data.

The regex ecosystem has two main branches today: POSIX and PCRE. Henry Spencer wrote the first portable regex library in C in 1986, which became the basis of the POSIX standard. In 1997, Philip Hazel wrote PCRE — Perl Compatible Regular Expressions — as a reusable C library, because Perl 5's regex had become so powerful (named capture groups, lookahead, lookbehind, substitutions) that everyone wanted it. PHP, R, and Nginx in `location` directives use PCRE. Python uses its own `re` module, inspired by Perl but with subtle differences. JavaScript used its own engine until ECMAScript 2018, which finally added named capture groups and lookbehind — features Perl had for decades. Go took a radically different position: it uses RE2, which guarantees linear execution time by eliminating catastrophic backtracking, but that means no lookahead or lookbehind.

Regex is the kind of knowledge that looks like hieroglyphics when you first encounter it, but becomes almost addictive once you learn to read it fluently. The problem is that patterns written without comments are notoriously difficult to maintain — a complete IPv4 validation regex runs over 60 characters and is completely opaque without context. For that there are verbose mode flags in Python (`re.VERBOSE`) and `x` mode in Ruby and PCRE, which allow inline comments. This library gathers the most commonly used patterns in daily work — email, CPF, CNPJ, postal code, Brazilian and international phone, credit card, URL, IPv4, IPv6, date, time — with real test cases and ready-to-paste snippets for PHP, JavaScript, TypeScript, Python, Go, Java, and C#. Use them as a starting point: every system has its own validation rules, and a copied-and-pasted regex rarely covers all the edge cases you will encounter in production.

Technical deep dive

Common questions summarized

  • What do the i, g, and m regex flags do?: The i flag ignores case. The g flag finds every match in the text (not only the first). The m flag makes ^ and $ match line starts/ends, not only the whole string.
  • Does this regex match test replace server-side validation?: No. Regex helps in the frontend and for prototyping, but business rules, security, and official formats (for example CPF check digits) should still be validated on the backend under your policies.
  • How do I copy the pattern for PHP or JavaScript?: Use the Regex field to copy the expression only. Under Ready-to-paste code by language, pick PHP, JavaScript, TypeScript, Python, Go, Java, or C# and copy the snippet with proper escaping and syntax for your project.
  • Do the CPF or credit card presets guarantee a valid number?: They usually validate shape (mask and characters) only. CPF, CNPJ, and cards need check-digit algorithms or payment networks: use these regexes as a first filter and add full validation where required.
  • What is this tool for?: It runs fully in your browser: useful to validate, format, or convert data in everyday development.
  • Are my inputs sent to a server?: Processing happens locally with JavaScript. We do not store what you paste into the text areas.
  • Can I use this for real production data?: Use at your own risk. For secrets (passwords, tokens), prefer controlled environments and your company policies. And always review the generated contents. Never trust blindly things you see on the internet.

Sample payload to try

  • See also the larger "Code Snippets" sample; paste this excerpt to try locally: Example — ^[^\s@]+@[^\s@]+\.[^\s@]+$

Tool guide

  • What regex is A pattern language used to validate and extract text fragments from forms, logs, and data pipelines.

  • What the tool manipulates Ready presets (email, Brazilian phone, date, strong password, CPF/CNPJ), custom pattern, flags, and test input.

  • What the tool does Evaluates match status, number of occurrences, and captured groups.

  • Why use it Build validation rules faster and avoid syntax mistakes while testing patterns.

Code Snippets

Code example
^[^\s@]+@[^\s@]+\.[^\s@]+$

Example

^[^\s@]+@[^\s@]+\.[^\s@]+$

FAQ

What do the i, g, and m regex flags do?

The i flag ignores case. The g flag finds every match in the text (not only the first). The m flag makes ^ and $ match line starts/ends, not only the whole string.

Does this regex match test replace server-side validation?

No. Regex helps in the frontend and for prototyping, but business rules, security, and official formats (for example CPF check digits) should still be validated on the backend under your policies.

How do I copy the pattern for PHP or JavaScript?

Use the Regex field to copy the expression only. Under Ready-to-paste code by language, pick PHP, JavaScript, TypeScript, Python, Go, Java, or C# and copy the snippet with proper escaping and syntax for your project.

Do the CPF or credit card presets guarantee a valid number?

They usually validate shape (mask and characters) only. CPF, CNPJ, and cards need check-digit algorithms or payment networks: use these regexes as a first filter and add full validation where required.

What is this tool for?

It runs fully in your browser: useful to validate, format, or convert data in everyday development.

Are my inputs sent to a server?

Processing happens locally with JavaScript. We do not store what you paste into the text areas.

Can I use this for real production data?

Use at your own risk. For secrets (passwords, tokens), prefer controlled environments and your company policies. And always review the generated contents. Never trust blindly things you see on the internet.