Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression for getting filename

Tags:

regex

How to write a regular expression for return file name by trim its extensions,

Following are my files,

externals/tinymce/plugins/emotions/img/smiley-smile.gif
externals/tinymce/plugins/emotions/img/smiley-laughing.gif
externals/tinymce/plugins/emotions/img/smiley-wink.gif
externals/tinymce/plugins/emotions/img/smiley-tongue-out.gif
externals/tinymce/plugins/emotions/img/smiley-cry.gif
externals/tinymce/plugins/emotions/img/smiley-surprised.gif
externals/tinymce/plugins/emotions/img/smiley-embarassed.gif

I would like to get filename without its extension and trim its smiley from staring using regular expresion,

Expected Outputs,

smile, laughing, wink, tongue-out, cry, surprised, embarassed
like image 747
Aadi Avatar asked Jun 30 '26 22:06

Aadi


2 Answers

You can use the regex:

.*smiley-(.*?)\.gif$

See it

The part you want will be in capture group 1.

like image 50
codaddict Avatar answered Jul 03 '26 14:07

codaddict


You can use the following regex to match the text that you are interested in:

/smiley-([^.]+)\.[^.]+$/

The smiley name will then be present in capture group 1.

like image 21
a'r Avatar answered Jul 03 '26 15:07

a'r