Regex Practice · 30 exercises

30 Regex Exercises with Solutions — Beginner to Advanced

Solve 30 real regex problems: number and text validation, email, dates, JavaScript, capturing groups and lookarounds. Learn common mistakes, reveal explanations and test each pattern in the Regex Playground.

Exercise collection or learning roadmap?

Here you solve 30 specific problems and inspect solutions. The practice roadmap is a separate guide for choosing which topics to train and designing your own tests.

How to practice Regex without memorizing syntax: Try each problem before opening the solution. Then test positive and negative cases in the Regex Tester. When it works, move into the 24-level game and keep your daily streak.

Pick the real-world problem you want to solve

Jump to the regex skill you need. Every exercise includes examples, an explanation and a Playground test.

Want to get confident with number regex?

Start with exercises 01, 02, 06, 16, and 17. They cover digits only, exact length, signed integers, and decimals.

01 · 02 · 06 · 16 · 17

Guide: Regex for Numbers

JavaScript Regex practice

Exercises 19, 20, 24, 28, and 29 cover capture, identifiers, named groups, and extraction — patterns that map naturally to JavaScript RegExp work.

19 · 20 · 24 · 28 · 29

Guide: JavaScript Regex

Beginner · 01–10

Build full-string patterns with anchors, classes and quantifiers.

BeginnerNumbers

01. Digits only

Match 12345; reject A123, 12-34, and an empty string.

Show solution and explanation
^\d+$

Anchors require the entire string to contain one or more digits.

Common mistake: Without ^ and $, the regex can match digits inside a longer value such as A123.

Test exercise 01 in the Playground
BeginnerNumbers

02. Exactly four digits

Match 2026; reject 26 and 20260.

Show solution and explanation
^\d{4}$

The {4} quantifier requires exactly four digits.

Common mistake: \d{4} without anchors may find four digits inside a longer sequence.

Test exercise 02 in the Playground
BeginnerText

03. ASCII letters only

Match RegexArena; reject Regex123 and regex-arena.

Show solution and explanation
^[A-Za-z]+$

This class covers ASCII letters; use Unicode-aware patterns when other alphabets matter.

Common mistake: [A-Za-z] does not automatically support accented letters or every Unicode script.

Test exercise 03 in the Playground
BeginnerCodes

04. DEV prefix with optional hyphen

Match DEV-2026 and DEV2026; reject DE-2026.

Show solution and explanation
^DEV-?\d{4}$

The ? makes the hyphen optional and \d{4} requires four digits.

Common mistake: The hyphen is optional, but DEV and the four digits are still mandatory.

Test exercise 04 in the Playground
BeginnerText

05. Simple slug

Match regex-arena and regex2026; reject spaces and duplicate hyphens.

Show solution and explanation
^[a-z0-9]+(?:-[a-z0-9]+)*$

Each additional block must be separated by a single hyphen.

Common mistake: Avoid leading and trailing hyphens; the non-capturing group keeps the slug structure.

Test exercise 05 in the Playground
BeginnerNumbers

06. Five to eight digits

Match 12345 and 12345678; reject shorter or longer values.

Show solution and explanation
^\d{5,8}$

The {5,8} quantifier sets minimum and maximum length.

Common mistake: The {5,8} range limits length, not the numeric value.

Test exercise 06 in the Playground
BeginnerLogs

07. Line starts with ERROR

Match ERROR connection lost; reject INFO ERROR.

Show solution and explanation
^ERROR\b

The start anchor fixes the position and  closes the word.

Common mistake: Without ^, ERROR could also match in the middle of a line.

Test exercise 07 in the Playground
BeginnerText

08. Repeated whitespace

Find one or more spaces, tabs, or line breaks so they can be replaced.

Show solution and explanation
\s+

\s represents whitespace and + captures a sequence.

Common mistake: \s includes tabs and newlines, not just ordinary spaces.

Test exercise 08 in the Playground
BeginnerAlternation

09. Cat or dog, singular/plural

Match cat, cats, dog, and dogs.

Show solution and explanation
^(cat|dog)s?$

Alternation selects cat/dog and s? makes the plural optional.

Common mistake: Without grouping, alternation can change the scope of anchors and suffixes.

Test exercise 09 in the Playground
BeginnerWeb

10. Hex color

Match #22aa77 and #FFFFFF; reject 22aa77.

Show solution and explanation
^#[0-9A-Fa-f]{6}$

The hash is literal and the class requires six hexadecimal digits.

Common mistake: This pattern accepts six-digit hex colors; three-digit shorthand needs another rule.

Test exercise 10 in the Playground

Intermediate · 11–20

Validate practical inputs and separate format checks from business rules.

IntermediateUS format

11. Brazilian postal code (CEP)

Match 94105 and 94105-1234; reject 9410.

Show solution and explanation
^\d{5}(?:-\d{4})?$

Five digits are required and the ZIP+4 suffix is optional.

Common mistake: A ZIP/CEP mask only validates the format, not whether an address exists.

Test exercise 11 in the Playground

This is the Brazilian CEP mask (five digits, a hyphen, then three digits), not the US ZIP code format.

IntermediateDates

12. DD/MM/YYYY date format

Match 11/10/2026; reject 2026-10-11.

Show solution and explanation
^\d{2}/\d{2}/\d{4}$

This checks visual format, not whether the calendar date exists.

Common mistake: A DD/MM/YYYY shape does not guarantee a real date, such as 31/02/2026.

Test exercise 12 in the Playground
IntermediatePhone

13. Brazilian mobile phone format

Match (415) 555-1234; reject 415 555-1234.

Show solution and explanation
^\(\d{3}\) \d{3}-\d{4}$

Literal parentheses must be escaped.

Common mistake: The pattern checks the displayed phone mask, not whether the number is active.

Test exercise 13 in the Playground

This checks the Brazilian (DD) 99999-9999 example format; national phone rules differ by country.

IntermediateEmail

14. Practical email shape

Match dev@example.com; reject dev@site and @example.com.

Show solution and explanation
^[\w.-]+@[\w.-]+\.[A-Za-z]{2,}$

Useful for practice, but not a complete implementation of email standards.

Common mistake: A practical email pattern does not cover the complete email address specification.

Test exercise 14 in the Playground
IntermediateURL

15. HTTP or HTTPS URL

Match https://regexarena.com/en/; reject ftp://example.com.

Show solution and explanation
^https?://[\w.-]+\.[A-Za-z]{2,}(?:/.*)?$

The optional s covers HTTP/HTTPS and the last group makes the path optional.

Common mistake: A valid-looking URL is not necessarily reachable or trustworthy.

Test exercise 15 in the Playground
IntermediateNumbers

16. Signed integer

Match 42, -42, and +42; reject 4.2.

Show solution and explanation
^[+-]?\d+$

The sign is optional and the rest must contain digits only.

Common mistake: An optional sign means + and - are allowed, but not required.

Test exercise 16 in the Playground
IntermediateNumbers

17. Decimal with dot or comma

Match 42, 42.50, and -42,50.

Show solution and explanation
^[+-]?\d+(?:[.,]\d+)?$

The decimal portion is optional and accepts a dot or comma.

Common mistake: Decide whether dot and comma decimal separators are both valid for your locale.

Test exercise 17 in the Playground
IntermediateLogs

18. Log level and date

Match lines beginning with INFO, WARN, or ERROR followed by YYYY-MM-DD.

Show solution and explanation
^(INFO|WARN|ERROR)\s+\d{4}-\d{2}-\d{2}

Alternation captures the log level and the second part requires the date.

Common mistake: Without an end condition, a log prefix may match while trailing text is ignored.

Test exercise 18 in the Playground
IntermediateGroups

19. Capture email user and domain

In dev@example.com, capture dev as group 1 and example.com as group 2.

Show solution and explanation
^([\w.-]+)@([\w.-]+\.[A-Za-z]{2,})$

Parentheses create capture groups that can be reused or inspected.

Common mistake: Captured groups separate user and domain; a full match alone does not extract them.

Test exercise 19 in the Playground
IntermediateJavaScript

20. Simple JavaScript identifier

Match userName, _cache, and $value; reject 2users.

Show solution and explanation
^[A-Za-z_$][\w$]*$

The first character has stricter rules; later characters may include word characters and $.

Common mistake: This simplified identifier pattern does not reject JavaScript reserved words.

Test exercise 20 in the Playground

Advanced · 21–30

Extract data and check context without consuming unnecessary text.

AdvancedLookahead

21. Positive lookahead

Capture DEV123 only when it is immediately followed by -OK.

Show solution and explanation
^DEV\d{3}(?=-OK$)

The lookahead checks context without consuming -OK.

Common mistake: A lookahead checks -OK without consuming those characters in the match.

Test exercise 21 in the Playground
AdvancedLookahead

22. Negative lookahead

Match lowercase names except the exact word admin.

Show solution and explanation
^(?!admin$)[a-z]+$

The negative lookahead excludes the reserved value before testing the rest.

Common mistake: The negative assertion excludes exact admin; case sensitivity is a separate choice.

Test exercise 22 in the Playground
AdvancedLookbehind

23. Price without currency

In $ 150, capture only 150.

Show solution and explanation
(?<=\$ )\d+$

The lookbehind requires the prefix without including it in the match.

Common mistake: Lookbehind requires the R$ prefix but does not include it in the match.

Test exercise 23 in the Playground
AdvancedGroups

24. Named group for a code

Capture DEV-2026 in a named group called code.

Show solution and explanation
(?<code>[A-Z]{3}-\d{4})

Named groups make extracted data easier to understand in JavaScript and the visualizer.

Common mistake: Named groups expose extracted values; they do not validate business rules by themselves.

Test exercise 24 in the Playground
AdvancedBackreference

25. Repeated word

Find the the or regex regex; reject regex arena.

Show solution and explanation
\b(\w+)\s+\1\b

 requires the second word to equal what group 1 captured.

Common mistake: A backreference requires the same captured word to appear again.

Test exercise 25 in the Playground
AdvancedValidation

26. Password with uppercase and digit

Require at least 8 characters, one uppercase letter, and one digit, using letters and digits only.

Show solution and explanation
^(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,}$

Two lookaheads verify requirements before the allowed character set is validated.

Common mistake: A shape check is not a complete password security policy.

Test exercise 26 in the Playground
AdvancedFiles

27. Image extension

Match files ending in jpg, jpeg, png, or webp using the i flag.

Show solution and explanation
^.+\.(?:jpe?g|png|webp)$

The non-capturing group collects extensions and jpe?g covers jpg/jpeg.

Common mistake: Use the i flag when .PNG and .JPG should also match; the extension is not file validation.

Test exercise 27 in the Playground
AdvancedGroups

28. Date with named groups

Capture day, month, and year from 11/10/2026 using named groups.

Show solution and explanation
^(?<day>\d{2})/(?<month>\d{2})/(?<year>\d{4})$

Each date part becomes available by name for later extraction.

Common mistake: Named groups extract day, month and year; date validity still requires another check.

Test exercise 28 in the Playground
AdvancedURL

29. Capture host and path

For an HTTP/HTTPS URL, capture the host as host and the optional path as path.

Show solution and explanation
^https?://(?<host>[\w.-]+\.[A-Za-z]{2,})(?<path>/.*)?$

Named groups split useful parts while still checking the overall format.

Common mistake: Extracting a host and path does not prove a URL can actually be reached.

Test exercise 29 in the Playground
AdvancedBoss

30. Context window

Capture DEV-2026 only after ID: and before ;OK.

Show solution and explanation
(?<=ID:)(?<code>[A-Z]{3}-\d{4})(?=;OK$)

Combine lookbehind, a named group, and lookahead in one challenge.

Common mistake: Lookbehind and lookahead check surrounding text without adding it to the match.

Test exercise 30 in the Playground

Keep practicing

Turn practice into a habit: test patterns, play missions, and use smart review to reinforce weaker concepts.

Open Regex TesterPlay 24 levels
Debugging practice

Practice with eight documented mistakes and four realistic developer workflows. Compare the broken pattern, the fix, and negative test cases before you reuse a solution.

Open the debugging lab