Aadi
Aadi

Reputation: 7109

Regular Expression for getting filename

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

Upvotes: 3

Views: 290

Answers (3)

codaddict
codaddict

Reputation: 455020

You can use the regex:

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

See it

The part you want will be in capture group 1.

Upvotes: 3

a'r
a'r

Reputation: 36999

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.

Upvotes: 2

canavanin
canavanin

Reputation: 2709

This should work:

/smiley-([\w-]+)\.gif/

The part within the parentheses is what you capture.

Upvotes: 0

Related Questions