.*
GUIA PASSO A PASSO

Aprenda Expressões Regulares

Um tutorial interativo e prático planejado para tornar a sintaxe de regex intuitiva e compreensível.

1. What is Regex?

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Think of it as a smart "find" tool — instead of searching for exact text, you search for patterns.

Regex is used everywhere in programming: - Validating input (emails, phone numbers, passwords) - Searching text (find all URLs in a document) - Extracting data (pull dates from a log file) - Find & replace (reformat text programmatically)

TRY ITThe simplest regex: a literal string match
/hello/gm
Say hello to the hello world!
Open in Validator →

2. Character Classes

Character classes let you match any one of a set of characters.

  • \d — any digit (0-9)
  • \w — any "word" character (letters, digits, underscore)
  • \s — any whitespace (spaces, tabs, newlines)
  • [abc] — any one of: a, b, or c
  • [a-z] — any lowercase letter
  • [^abc] — any character except a, b, or c
  • . (dot) — any character except newline

Uppercase versions are the opposite: \D = non-digit, \W = non-word, \S = non-whitespace.

TRY IT\d matches digits, {3} means exactly 3
/\d{3}-\d{4}/gm
Call me at 555-1234 or 800-9999
Open in Validator →

3. Quantifiers

Quantifiers specify how many of the preceding element to match.

QuantifierMeaning
*0 or more
+1 or more
?0 or 1 (optional)
{n}Exactly n
{n,}n or more
{n,m}Between n and m

By default, quantifiers are greedy (match as much as possible). Add ? to make them lazy (match as little as possible).

TRY IT? makes the "u" optional
/colou?r/gm
Is it color or colour? Both are correct colors.
Open in Validator →

4. Anchors & Boundaries

Anchors don't match characters — they match positions.

  • ^ — start of string (or line, with m flag)
  • $ — end of string (or line, with m flag)
  • \b — word boundary (between \w and \W)
  • \B — non-word boundary

These are essential for ensuring your regex matches the right thing, not just any occurrence.

TRY IT^\w+ matches the first word of each line (with m flag)
/^\w+/gm
First word Second word Third word
Open in Validator →

5. Groups & Capturing

Parentheses create groups that can capture matched text.

  • (abc) — capturing group (stores the match)
  • (?:abc) — non-capturing group (groups without storing)
  • (?abc) — named group (access by name)
  • \1 — backreference (match the same text as group 1)

Groups are also used with quantifiers: (ha)+ matches "ha", "haha", "hahaha".

TRY ITGroups capture the username and domain separately
/(\w+)@(\w+\.\w+)/gm
Contact: user@example.com or admin@test.org
Open in Validator →

6. Alternation (OR)

The pipe | acts as an OR operator.

  • cat|dog — matches "cat" or "dog"
  • (red|blue|green) car — matches "red car", "blue car", or "green car"

Without parentheses, alternation applies to the entire pattern on each side. Use groups to limit the scope.

TRY ITMatches files ending in .js, .ts, .py, or .go
/\.(js|ts|py|go)$/gm
app.js style.css main.py server.go index.html
Open in Validator →

7. Lookahead & Lookbehind

Lookaround assertions check what's around a match without including it.

  • (?=abc) — positive lookahead: followed by "abc"
  • (?!abc) — negative lookahead: NOT followed by "abc"
  • (?<=abc) — positive lookbehind: preceded by "abc"
  • (? — negative lookbehind: NOT preceded by "abc"

These are powerful for extracting text in specific contexts.

TRY ITMatches numbers followed by "px" (without capturing "px")
/\d+(?=px)/gm
font-size: 16px; margin: 24px; width: 100%;
Open in Validator →

8. Flags

Flags modify how the regex engine processes the pattern.

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitiveIgnore letter casing
mMultiline^ and $ match line boundaries
sDotall. matches newline characters
uUnicodeFull Unicode support
dIndicesProvides match index info
yStickyMatch from lastIndex position
TRY ITWith the "i" flag, all variations match
/hello/gm
Hello HELLO hello hElLo
Open in Validator →

You're ready!

You now know the fundamentals of regex. Practice with our validator or explore common patterns.