Regex for file name without extension

I am looking for a regex expression that will write the name of a file that does not have an extension, and give me that name in the opposite direction so that I can add the extension. Therefore, if someone puts xyz, I can replace it with xyz.html. xyz.php or xyz.html should not be captured.

thanks

+8
regex
source share
2 answers

Use the following regular expression:

^([^.]+)$ 

means a file name consisting of non-dot characters only.

UPDATE

added paren to capture the group.

+10
source share

Assuming extensions are up to 4 characters long (therefore file names such as mr.smith are not considered to have an extension, but mr.smith.doc and mr.smith.html considered to have extensions):

 ^.*[^.]{5}$ 

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

+3
source share

All Articles