Regexdrill

Level 4: Extraction and cleanup Exercise 57 of 88 Free

File extensions

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

Each line is a file name or a path. Capture the extension in group 1: the letters and digits after the last period, at the very end of the line. Lines that do not end in a period followed by letters or digits give no match.

What this exercise teaches

\.([A-Za-z0-9]+)$ works backwards from the end: the $ fixes the extension to the end of the line and the class keeps periods and slashes out of it. Without the $, the first period wins and archive.tar.gz gives tar.

A hint if you get stuck

Anchor the pattern to the end of the line with $. A class of letters and digits cannot contain another period, so the match has to start at the last one.

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 Extraction and cleanup