ka.tee.jean
ka.tee.jean

Reputation: 197

REGEX for any file extension

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.

Upvotes: 9

Views: 34044

Answers (8)

Vladislav Velichko
Vladislav Velichko

Reputation: 1

^\.\w+$ should work
^ - begin of line starts with \. - dot, then \w (equivalent to [a-zA-Z0-9_]) until the and of string $

Upvotes: 0

Eugene P.
Eugene P.

Reputation: 1823

If you are looking for a regex to get the file extension from a filename, here it is

(?<=\.)[^.\s]+$

Upvotes: 0

Adarsh
Adarsh

Reputation: 31

This works for me in javascript

^[.][a-zA-Z0-9.,$;]+$

Upvotes: 1

Nae
Nae

Reputation: 15335

I use:

(?:.*\\)+([^\\]+)

for Windows for it produces short filename with extension.

Upvotes: 0

JJ-
JJ-

Reputation: 21

If you already have a string like ".hello" with the extension and you're just testing it to see if it matches then you can try something like ^\.[^\\/:*?"<>|\s.]{1,255}$. It works with all of your example cases.

The beginning ^\. means the whole string must start with a literal dot "."

The [^\\/:*?"<>|\s.] means that after the dot you can have any character except a backslash, forward slash, colon, asterisk, question mark, double quotation mark, less than or greater than symbol, vertical bar, whitespace character, or dot. Feel free to add whatever other characters you'd like to disallow inside of the square brackets after the carrot or delete any characters that I added that you wish to allow.
(Note: the allowable characters for filenames/extensions depends on the file system.)

The {1,255}$ at the end quantifies the amount of allowable characters that we just defined all the way until the end of the string. So anything that's after the dot and allowed can be between 1 and 255 characters long and it must go on until the end of the string. Feel free to change the 255 to any number that you like.
(Note: the maximum length for filenames/extensions depends on the file system.)

If you are searching a string like "https://sub.example.com/directory1/directory2/file.php" for the file extension you should instead use \.[^\\/:*?"<>|\s.]{1,255}$ to search for the final extension including the dot.

Upvotes: 2

Hunter Eidson
Hunter Eidson

Reputation: 1909

Try this regex:

^\.[\w]+$

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

Upvotes: 5

darthmaim
darthmaim

Reputation: 5148

Try this regex, which matches all strings starting with a dot followed by at least one other character:

^\.[^.]+$

Upvotes: 2

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

^\.[^.]+$

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]+$

Upvotes: 15

Related Questions