Regexdrill

Level 2: Groups and alternation Exercise 13 of 52 Pro

Capture a standalone year

A regex exercise from Regexdrill. Solve it with a pattern box with live highlighting in your browser, checked against hidden test strings, and it is checked the moment you submit.

The task

Capture the first standalone four-digit number on each line in group 1. Digits that are part of a longer number do not count. Lines without one give no match.

What this exercise teaches

\b(\d{4})\b captures four digits with a boundary on each side, so 12345 is skipped instead of yielding 1234.

A hint if you get stuck

Parentheses capture. \b marks a word boundary, the edge between a word character and a non-word character.

This exercise is part of Pro

Pro unlocks all 52 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Groups and alternation