Regexdrill

Level 2: Groups and alternation Exercise 30 of 88 Free

Nested groups: a time and its hour

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

Find the first time written HH:MM, with two digits on each side of the colon. Capture the whole time in group 1 and only the hours in group 2. Groups are numbered by their opening parenthesis, so the outer group comes first.

What this exercise teaches

In ((\d{2}):\d{2}) the outer parenthesis opens first, so it is group 1 and holds the whole time. The inner one opens second and is group 2. Counting opening parentheses from the left always gives the group number.

A hint if you get stuck

Put one pair of parentheses around the whole time, and a second pair inside it around the hours.

Try it now, free, no account needed

Every exercise on Regexdrill is free to try. Open it on the practice page, solve it, and get instant feedback with a hint. The worked solution opens once you solve it or after two tries.

Open this exercise

More exercises in Groups and alternation