Regex practice

Regex Exercises: 18 Practice Problems with Solutions

Practice regular expressions from basic anchors and quantifiers to groups, backreferences, lookahead, and lookbehind. Try each problem before opening the solution.

Best way to practice: write your pattern in the Regex Tester, include examples that should fail, and only reveal the solution after you have tested your own idea.
18 problems

Beginner → Intermediate → Advanced

These practice questions are designed to build one concept at a time and then combine concepts in the final problems.

Beginner

1. Numbers only

Match a string containing only one or more digits.

Should match: 12345
Should reject: A123, 12-34

Show solution
^\d+$

Anchors force the whole value to be digits.

Beginner

2. Starts with DEV-

Match DEV- followed by exactly four digits.

Should match: DEV-2026
Should reject: DEV-26, XDEV-2026

Show solution
^DEV-\d{4}$

Literal text plus an exact digit quantifier.

Beginner

3. Three-letter file extension

Match file names ending in .jpg, .png, or .gif, ignoring case.

Should match: photo.JPG
Should reject: photo.pdf

Show solution
\.(?:jpg|png|gif)$

Escape the dot and anchor the extension at the end.

Beginner

4. Letters only

Match ASCII letters only.

Should match: RegexArena
Should reject: Regex2026

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

A character class plus anchors validates the entire value.

Beginner

5. Optional color spelling

Match both color and colour.

Should match: color, colour
Should reject: colouur

Show solution
^colou?r$

The question mark makes the preceding u optional.

Beginner

6. Exactly two letters and two digits

Match AB12-style codes.

Should match: AB12
Should reject: A123, ab12

Show solution
^[A-Z]{2}\d{2}$

Use exact quantifiers for the two sections.

Intermediate

7. Date format

Match DD/MM/YYYY format only.

Should match: 24/09/2026
Should reject: 2026-09-24

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

This checks format, not whether the calendar date exists.

Intermediate

8. Simple email shape

Match a practical email-shaped value.

Should match: dev@example.com
Should reject: dev@site, @example.com

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

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

Intermediate

9. HTTP or HTTPS URL

Match a simple http/https URL with an optional path.

Should match: https://regexarena.com/en/
Should reject: ftp://example.com

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

The s is optional; the path group is optional.

Intermediate

10. Log level and date

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

Should match: ERROR 2026-09-24 Failed
Should reject: DEBUG 2026-09-24 Trace

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

Alternation captures the allowed log level.

Intermediate

11. Extract hashtags

Find every hashtag made of word characters.

Should match: #regex #javascript
Should reject: plain text

Show solution
#\w+

Use the global flag in JavaScript to collect all occurrences.

Intermediate

12. Collapse repeated whitespace

Find one or more whitespace characters for replacement.

Should match: a b
Should reject: ab

Show solution
\s+

Replace each match with a single space.

Advanced

13. Positive lookahead

Capture DEV plus three digits only when it is immediately followed by -OK.

Should match: DEV123-OK
Should reject: DEV123-NO

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

The lookahead checks context without consuming -OK.

Advanced

14. Negative lookahead

Match lowercase names except the exact word admin.

Should match: marcello, regexarena
Should reject: admin

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

The negative lookahead rejects one reserved value.

Advanced

15. Positive lookbehind

Capture digits only when preceded by R$ and a space.

Should match: R$ 150
Should reject: USD 150

Show solution
(?<=R\$ )\d+$

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

Advanced

16. Named capture group

Capture a code such as DEV-2026 in a named group called code.

Should match: DEV-2026
Should reject: DE-2026

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

Named groups make extracted data easier to understand.

Advanced

17. Repeated words

Find a word repeated immediately after itself.

Should match: the the
Should reject: the regex

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

The backreference \1 repeats the text captured by group 1.

Advanced

18. Context window

Capture AAA-0000 only after ID: and before ;OK.

Should match: ID:DEV-2026;OK
Should reject: ID:DEV-2026;FAIL

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

Combine lookbehind, a named group, and lookahead.

Want automatic validation and progression?

RegexArena has 24 interactive levels with positive and negative cases, score, XP, progressive hints, capture-group feedback, and dedicated lookahead/lookbehind missions.

Play 24 levelsOpen Regex Tester

What to study next