Am I putting regex the correct way here to filter out image filename and filetype?
^(\w)+\.(jpg|png|gif)$
the link to the exercise is here:
http://regexone.com/example/4?
The exercise is To capture the name and the extension from a list of files:
task text capture
skip text .bash_profile
skip text workspace.doc
capture text img0912.jpg img0912, jpg
capture text updated_img0912.png updated_img0912, png
skip text documentation.html
capture text favicon.gif favicon, gif
skip text img0912.jpg.tmp
skip text access.lock
You have only one mistake:
You capture only the last character of the filename with
(\w)+. This is called “repeat a capturing group“, that is not what you want here. Put the quantifier inside the group to capture the complete name.Some notes
The starting anchor “^” is not necessarily needed, depends on what you are looking for. For that example it would make no difference.
\wworks for your example, but will not capture all valid filenames, because it contains only Letters, Digits and the Underscore.