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 exerciseMore exercises in Extraction and cleanup
- Log lines: level and messageFree
- key=value pairsFree
- The third CSV fieldFree
- HashtagsFree
- Host names from URLsFree
- A query string parameterFree
- Trim whitespaceFree
- Collapse repeated spacesFree
- Reformat MM/DD/YYYY as YYYY-MM-DDFree
- Swap Last, First to First LastFree
- Split camelCase with underscoresFree
- Strip HTML tagsFree
- Keep only the digitsFree
- CSV fields with quotesFree
- Normalize phone numbersFree
- Link text inside <a> tagsFree
- Web server access logFree