Independent practice

10 Regex challenges with test cases and explanations

Go beyond the 24 guided levels. These independent problems ask you to build a Regex, test what should and should not match, then compare your reasoning with a worked solution.

Independent challenges or guided exercise list?

Try these problems before checking the solution. If you prefer structured problems with explanations by difficulty, switch to the exercise collection.

How to use these challenges

  1. Read the rule and predict which test strings should match.
  2. Try your own Regex in the Playground, without revealing the solution.
  3. Test the negative example; if it matches, refine your pattern.
  4. Open the worked solution to understand its trade-offs. Some exercises validate a format, not real-world identity or security.
Challenge 01 / 10

Image file extensions

Accept familiar image files and reject double extensions.

Should match: picture.JPG

Should not match: image.jpg.exe

Hint β€” solve it yourself first

Start from the end of the filename, group acceptable extensions, and enable the case-insensitive flag.

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

The escaped dot matches a real period. The final anchor stops an attacker or accidental suffix such as .exe.

Flags: gi. More than one pattern may be correct.

Try the example in Playground
Challenge 02 / 10

Password character policy

Check a simple format of 8–16 ASCII letters, digits or underscores; this is not a password-strength test.

Should match: Dev_pass24

Should not match: hello!

Hint β€” solve it yourself first

Define the allowed alphabet first; anchor both ends; then constrain the length.

See a worked solution and explanation
^[A-Za-z0-9_]{8,16}$

Anchors reject extra characters and the quantifier sets the length. Password strength also requires checks for compromised or predictable passwords.

Flags: g. More than one pattern may be correct.

Try the example in Playground
Challenge 03 / 10

IPv4 address with valid octets

Match IPv4 dotted decimal values from 0 through 255 without padded octets.

Should match: 192.168.1.10

Should not match: 999.168.1.10

Hint β€” solve it yourself first

Create one octet alternative and reuse it four times, with literal dots.

See a worked solution and explanation
^(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$

Alternatives represent 250–255, 200–249, 100–199 and 0–99. For production networking software, prefer a dedicated IP parser.

Flags: g. More than one pattern may be correct.

Try the example in Playground
Challenge 04 / 10

Order number

Allow ORD followed by four digits, a hyphen and two uppercase letters.

Should match: ORD-2048-BA

Should not match: ORD-2048-ba

Hint β€” solve it yourself first

Write the literal prefix and separator, then add quantified character classes.

See a worked solution and explanation
^ORD-\d{4}-[A-Z]{2}$

The prefix and separators are literals; digits and uppercase letters are controlled separately.

Flags: g. More than one pattern may be correct.

Try the example in Playground
Challenge 05 / 10

Inventory SKU

Accept the BK, EL or HM categories and a two-letter plus three-digit product code.

Should match: EL-AB-123

Should not match: XX-AB-123

Hint β€” solve it yourself first

Use alternation for categories and a noncapturing group for readable structure.

See a worked solution and explanation
^(?:BK|EL|HM)-[A-Z]{2}-\d{3}$

A noncapturing group limits categories without adding a result group. Anchors enforce the whole value.

Flags: g. More than one pattern may be correct.

Try the example in Playground
Challenge 06 / 10

Email with named fields

Extract user and domain from common, simple email shapes.

Should match: sam.lee@example.com

Should not match: sam@example

Hint β€” solve it yourself first

Create separate captures around the @ separator, then test a missing top-level domain.

See a worked solution and explanation
^(?<user>[A-Za-z0-9._%+-]+)@(?<domain>[A-Za-z0-9.-]+\.[A-Za-z]{2,})$

Named groups make results easier to read. This is a teaching pattern, not full RFC email verification; real sign-up flows should also confirm the address.

Flags: g. More than one pattern may be correct.

Try the example in Playground
Challenge 07 / 10

Version number

Accept three dot-separated nonnegative numeric components in a simplified version string.

Should match: 2.14.0

Should not match: 2.14

Hint β€” solve it yourself first

Escape literal dots and explicitly repeat the three numeric blocks.

See a worked solution and explanation
^\d+\.\d+\.\d+$

This checks a simplified version shape, not the complete semantic-versioning specification (pre-release labels and leading-zero restrictions are omitted).

Flags: g. More than one pattern may be correct.

Try the example in Playground
Challenge 08 / 10

Amount in a status message

Extract the amount only when it follows R$ and precedes ;OK.

Should match: Total R$ 120,00;OK

Should not match: Total USD 120,00;OK

Hint β€” solve it yourself first

Begin with the numeric value; add contextual assertions on both sides.

See a worked solution and explanation
(?<=R\$ )\d+(?:,\d{2})?(?=;OK)

Lookbehind and lookahead verify context without including it in the extracted match. This assumes a modern JavaScript engine.

Flags: g. More than one pattern may be correct.

Try the example in Playground
Challenge 09 / 10

Critical logs

Extract a request ID from lines starting with ERROR, ignoring INFO/WARN.

Should match: ERROR requestId=ab-123 Timeout

Should not match: INFO requestId=ab-123 OK

Hint β€” solve it yourself first

Start from the log level, then capture only the identifier you need.

See a worked solution and explanation
^ERROR\s+requestId=(?<id>[a-z]{2}-\d{3})\b

The line-start anchor and m flag restrict extraction to ERROR lines. A named group isolates the request ID.

Flags: gm. More than one pattern may be correct.

Try the example in Playground
Challenge 10 / 10

Final boss: contextual extraction

Capture a three-letter code and four digits only after ID: and before ;OK.

Should match: ID:DEV-2026;OK

Should not match: ID:DEV-2026;FAIL

Hint β€” solve it yourself first

Combine a positive lookbehind, named capture and positive lookahead.

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

The captured match excludes ID: and ;OK. The trailing anchor prevents accepting additional text after the status.

Flags: g. More than one pattern may be correct.

Try the example in Playground

Continue with guided practice

Not ready for open-ended problems? The game provides hints and feedback on false positives and false negatives. The exercise library adds 30 progressive problems with solutions.

Play the 24 levelsExplore 30 exercisesReview the guide
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