Regex Practice · 30 exercises

30 Regex Exercises with Solutions — Beginner to Advanced

Practice regex through short problems, real examples, and explained solutions. Start with numbers and simple patterns, then move into groups, JavaScript, lookarounds, and extraction.

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.

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
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.

BeginnerNumbers

02. Exactly four digits

Match 2026; reject 26 and 20260.

Show solution and explanation
^\d{4}$

The {4} quantifier requires exactly four digits.

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.

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.

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.

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.

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.

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.

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.

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.

IntermediateUS format

11. ZIP code

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.

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.

IntermediatePhone

13. Formatted phone number

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

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

Literal parentheses must be escaped.

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.

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.

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.

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.

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.

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.

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 $.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Keep practicing

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

Open Regex TesterPlay 24 levels