Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REGEX for any file extension

Tags:

regex

I am trying to build a regex to tell if a string is a valid file extension. It could be any extentions.

hello        no
.hello       Yes
..hello      No
hello.world  No
.hello.world No
.hello world No

I have tried ^\. and ^\.[\.] but can't get what i am looking for. This seems like it should be simple.

like image 856
ka.tee.jean Avatar asked Mar 06 '14 20:03

ka.tee.jean


2 Answers

^\.[^.]+$

This means start with . and then anything other than dot (.)

You can also use this one if you want to have only aplhanumeric.:

^\.[a-zA-Z0-9]+$
like image 167
Sabuj Hassan Avatar answered Sep 18 '22 15:09

Sabuj Hassan


Try this regex:

^\.[\w]+$

Matches a string starting with a ".", followed by one or more "word" character(s), until the end of the string.

like image 30
Hunter Eidson Avatar answered Sep 20 '22 15:09

Hunter Eidson