2024 © RegexOne
Email |
Twitter
If you use Linux or the command line frequently, are often dealing with lists of files. Most files have a filename component as well as an extension, but in Linux, it is also common to have hidden files that have no filename.
In this simple example, extract the filenames and extension types of only image files (not including temporary files for images currently being edited). Image files are defined as .jpg,.png, and .gif.
Task | Text | Capture Groups | |
skip | .bash_profile | ||
skip | workspace.doc | ||
capture | img0912.jpg | img0912 jpg | |
capture | updated_img0912.png | updated_img0912 png | |
skip | documentation.html | ||
capture | favicon.gif | favicon gif | |
skip | img0912.jpg.tmp | ||
skip | access.lock |
Solution | We are only looking for image files ending with the 'jpg', 'png' and 'gif' file extensions, so we can capture all such filenames using the expression (\w+)\.(jpg|png|gif)$. |