Regex tester with Brazilian patterns
Run a JavaScript regex against your text and see the matches, the groups and the result of a substitution. Ready-made patterns cover CPF, CNPJ (including the alphanumeric one), CEP, phone numbers and Mercosul license plates, to validate a form mask or pull data out of a log. Everything runs in your browser.
If you paste a regex written as /pattern/flags, the flags move to the buttons below.
Each line is treated as its own value, like a form field. That makes ^ and $ apply to the line without the m flag.
Type a regex
Write the regex and the text, or pick a Brazilian pattern below.
Brazilian patterns
Pick the document, the format, and whether you want to validate a whole field or extract from a text. The regex shows up ready to copy or to test here.
^(?:\d{3}\.\d{3}\.\d{3}-\d{2}|\d{11})$Flags: -- Format only: the regex does not compute the check digit. The badge in the results uses the same calculation as the validator on this site.
Need values to test the CPF pattern?
Regex for Brazilian documents
A regex only checks whether the text has the right shape. CPF, CNPJ, CEP, phone numbers and plates have well defined formats, so they fit a regex well, but the number can still be fake: the check digit needs a calculation a regex cannot do. The patterns on this page are for validating a field mask, where the regex covers the whole value, or for extracting documents from a log, finding each one in the middle of the text without cutting into a longer number.
What each pattern accepts
The table shows the format of each pattern. Punctuation can be required, forbidden or optional, and the format changes with the variation you pick.
| Document | Accepts |
|---|---|
| CPF | 000.000.000-00 or 00000000000 |
| CNPJ | 00.000.000/0000-00 or 14 digits; in the new format, uppercase letters and digits in the first 12 positions |
| CEP | 00000-000 or 00000000 |
| Phone | (11) 91234-5678 or 11912345678, with an optional +55; 8-digit landlines and old mobiles too |
| License plate | ABC1D23 (Mercosul) and ABC-1234 (old) |
| PIS/PASEP | 000.00000.00-0 or 11 digits |
| Date | dd/mm/yyyy or ddmmyyyy |
| Amount in reais | R$ 1.234,56 or R$ 1234,56 |
Validating a field or extracting from a text
When validating, the regex starts with ^ and ends with $, so the whole value has to match, and a value with a stray line break is refused because the pattern does not use the m flag. When extracting, the regex uses the g flag and looks at what comes before and after: an 11-digit CPF is not pulled out of the middle of a 13-digit number. To test a list of values at once, use the per-line test.
This is JavaScript regex
The tester uses your browser's regex engine. In Python, Java, PHP or PCRE the syntax differs in a few places: the named group is (?P<name>...) in Python, Java needs double backslashes inside strings and some engines limit lookbehind. The Brazilian patterns use lookbehind for extraction, so check them in your engine before copying.
Stuck regex and privacy
The regex runs in a Web Worker with a 2 second limit. If it goes over, the worker is stopped and the tab keeps responding. The text and the regex stay in the browser's memory only: they do not go to a server, the page address, usage metrics or local storage.
Questions aboutregex and documents
Common questions from people who validate forms and extract data from logs in Brazil.