Folha de Referência Regex

Referência interativa de regex com guia de sintaxe pesquisável. Teste padrões com correspondência ao vivo. Abrange classes de caracteres, quantificadores, grupos e sinalizadores.

Showing 39 of 39 entries

Character Classes

10 entries
.

Any character

Matches any single character except newline (unless s flag is set)

Pattern: c.tMatches: "cat", "cut", "c3t"
\d

Digit

Matches any digit character [0-9]

Pattern: \d+Matches: "42", "007"
\D

Non-digit

Matches any character that is not a digit

Pattern: \D+Matches: "abc", "foo!"
\w

Word character

Matches any alphanumeric character or underscore [A-Za-z0-9_]

Pattern: \w+Matches: "hello", "foo_42"
\W

Non-word character

Matches any character that is not a word character

Pattern: \W+Matches: " ", "!@#"
\s

Whitespace

Matches any whitespace character: space, tab, newline, carriage return, form feed

Pattern: \s+Matches: " ", "\t\n"
\S

Non-whitespace

Matches any character that is not whitespace

Pattern: \S+Matches: "hello", "word"
[abc]

Character set

Matches any single character listed inside the brackets

Pattern: [aeiou]Matches: "a", "e", "i", "o", "u"
[^abc]

Negated set

Matches any single character NOT listed inside the brackets

Pattern: [^aeiou]Matches: "b", "c", "d" (not vowels)
[a-z]

Character range

Matches any character in the specified Unicode range. Combine ranges: [a-zA-Z0-9]

Pattern: [a-z]+Matches: "hello", "world"

Anchors

4 entries
^

Start of string

Matches the position at the start of the string (or line with m flag)

Pattern: ^HelloMatches: "Hello world" (start only)
$

End of string

Matches the position at the end of the string (or line with m flag)

Pattern: world$Matches: "Hello world" (end only)
\b

Word boundary

Matches the position between a word character and a non-word character

Pattern: \bcat\bMatches: "cat" but not "catfish"
\B

Non-word boundary

Matches any position that is not a word boundary

Pattern: \Bcat\BMatches: "concatenate" (mid-word)

Quantifiers

8 entries
*

Zero or more (greedy)

Matches the preceding element zero or more times, as many as possible

Pattern: ab*cMatches: "ac", "abc", "abbc"
+

One or more (greedy)

Matches the preceding element one or more times, as many as possible

Pattern: ab+cMatches: "abc", "abbc" (not "ac")
?

Zero or one

Matches the preceding element zero or one time (makes it optional)

Pattern: colou?rMatches: "color", "colour"
{n}

Exactly n

Matches the preceding element exactly n times

Pattern: \d{4}Matches: "2024", "1234"
{n,}

n or more

Matches the preceding element n or more times

Pattern: \d{2,}Matches: "12", "999", "1000"
{n,m}

Between n and m

Matches the preceding element between n and m times (inclusive)

Pattern: \d{2,4}Matches: "12", "123", "1234"
*?

Zero or more (lazy)

Matches the preceding element zero or more times, as few as possible

Pattern: <.+?>Matches: "<b>" in "<b>bold</b>"
+?

One or more (lazy)

Matches the preceding element one or more times, as few as possible

Pattern: "[^"]+?"Matches: Shortest quoted string

Groups

6 entries
(abc)

Capturing group

Groups the expression and captures the matched text. Accessible as \1, \2, or match[1]

Pattern: (\d{4})Matches: Captures year in "2024-01-15"
(?:abc)

Non-capturing group

Groups the expression without creating a capture. Use to apply quantifiers without capturing

Pattern: (?:ab)+Matches: "ab", "abab", "ababab"
(?=abc)

Positive lookahead

Asserts that what follows the current position matches the pattern (zero-width)

Pattern: \d+(?= USD)Matches: "100" in "100 USD"
(?!abc)

Negative lookahead

Asserts that what follows does NOT match the pattern (zero-width)

Pattern: \d+(?! USD)Matches: "42" in "42 EUR"
(?<=abc)

Positive lookbehind

Asserts that what precedes the current position matches the pattern (zero-width)

Pattern: (?<=\$)\d+Matches: "100" in "$100"
(?<!abc)

Negative lookbehind

Asserts that what precedes does NOT match the pattern (zero-width)

Pattern: (?<!\$)\d+Matches: "42" but not "$42"

Flags

6 entries
g

Global

Find all matches rather than stopping after the first match

Pattern: /\d+/gMatches: All numbers in a string
i

Ignore case

Makes the pattern case-insensitive

Pattern: /hello/iMatches: "hello", "HELLO", "Hello"
m

Multiline

Makes ^ match the start of each line and $ match the end of each line

Pattern: /^\w+/mMatches: First word of each line
s

DotAll

Makes . match any character including newline characters

Pattern: /a.b/sMatches: "a\nb" across newlines
u

Unicode

Enables full Unicode matching; required for \p{...} property escapes and 4-byte Unicode code points

Pattern: /\p{L}+/uMatches: Any Unicode letter
y

Sticky

Matches only from the position indicated by lastIndex; does not advance past it

Pattern: /\d+/yMatches: Only at exact lastIndex position

Special

5 entries
|

Alternation

Matches either the expression before or after the pipe. Acts like OR

Pattern: cat|dogMatches: "cat" or "dog"
\1

Backreference

Matches the same text that was matched by capturing group 1. \2 for group 2, etc.

Pattern: (\w+)\s\1Matches: "the the", "ha ha"
\n

Newline

Matches a newline character (line feed, LF)

Pattern: \r?\nMatches: Unix or Windows line endings
\t

Tab

Matches a horizontal tab character

Pattern: \t+Matches: Tab-indented lines
\\

Literal backslash

Matches a literal backslash character. Backslash must be escaped as \\

Pattern: C:\\\w+Matches: "C:\Users" in file paths

Quick Test

— paste a pattern and see matches highlighted
//g

Como Usar Folha de Referência Regex

  1. 1Procure a sintaxe regex por categoria.
  2. 2Pesquise padrões ou conceitos específicos.
  3. 3Teste seu regex com o comparador ao vivo.
  4. 4Copie qualquer padrão para usar em seu código.
ZenovayAnalytics

Analytics feito para fundadores.

  • Acompanhamento de visitantes em tempo real
  • Privacidade em primeiro lugar, sem banner de cookies
  • Configurado em dois minutos
Conheça o Zenovay

Perguntas Frequentes

O que é uma expressão regular (regex)?
Uma expressão regular é uma sequência de caracteres que define um padrão de busca. Regex é usada para correspondência de strings, validação, pesquisa e substituição e extração de texto. É suportada em praticamente todas as linguagens de programação.
Quais são os diferentes sabores de regex?
Principais sabores: PCRE (PHP, Python re), JavaScript (ECMAScript), .NET, Java, POSIX. Diferem em recursos como lookbehind, grupos nomeados e suporte Unicode. O regex JavaScript adicionou recursos como grupos nomeados e lookbehind em ES2018.
O que é correspondência gulosa vs preguiçosa?
Os quantificadores gulosos (*, +, ?) correspondem o máximo possível. Os quantificadores preguiçosos (não gulosos) (*?, +?, ??) correspondem o mínimo possível. Exemplo: para '<b>bold</b>', /<.*>/ corresponde '<b>bold</b>' (guloso) enquanto /<.*?>/ corresponde '<b>' (preguiçoso).
O que são lookahead e lookbehind?
Lookahead (?=...) e lookbehind (?<=...) são asserções de largura zero que combinam sem consumir caracteres. Lookahead positivo: \d(?=px) corresponde a dígitos seguidos por 'px'. Lookbehind negativo: (?<!\$)\d+ corresponde a números não precedidos por '$'.
Quais são os erros comuns de regex?
Não escapar caracteres especiais (. corresponde a qualquer caractere, não ponto literal). Backtracking catastrófico com quantificadores aninhados. Usar .* quando um padrão mais específico funcionaria. Não ancorar padrões (esquecendo ^ e $). Usar regex para análise de HTML/XML em vez de um analisador adequado.