Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for a file name without an extension

Tags:

regex

I'm looking for a regex expression that will capture a file name that does not have an extension and give me that name in a backreference so I can add an extension. So if someone puts in xyz I can replace it with xyz.html. xyz.php or xyz.html should not be captured.

Thanks

like image 569
Steve Avatar asked Aug 02 '13 13:08

Steve


2 Answers

Assuming the extensions are up to 4 chars in length (so filenames like mr.smith aren't considered as having an extension, but mr.smith.doc and mr.smith.html are considered as having extensions):

^.*[^.]{5}$

No need to capture a group, as the whole expression is what you want - ie group 0.

like image 161
Bohemian Avatar answered Oct 22 '22 14:10

Bohemian


Use following regular expression:

^([^.]+)$

meaning filename that only consist of non-dot characters.

UPDATE

added paren for capturing group.

like image 21
falsetru Avatar answered Oct 22 '22 16:10

falsetru