Regex for finding valid filename
Solution 1
Here you go:
"[^/?*:;{}\\]+\\.[^/?*:;{}\\]+"
"One or more characters that aren't any of these ones, then a dot, then some more characters that aren't these ones."
(As long as you're sure that the dot is really required - if not, it's simply: "[^/?*:;{}\\]+"
Solution 2
$a = preg_match('=^[^/?*;:{}\\\\]+\.[^/?*;:{}\\\\]+$=', 'file.abc');
^ ... $ - begin and end of the string
[^ ... ] - matches NOT the listed chars.
Solution 3
The regex would be something like (for a three letter extension):
^[^/?*:;{}\\]+\.[^/?*:;{}\\]{3}$
PHP needs backslashes escaped, and preg_match()
needs forward slashes escaped, so:
$pattern = "/^[^\\/?*:;{}\\\\]+\\.[^\\/?*:;{}\\\\]{3}$/";
To match filenames like "hosts"
or ".htaccess"
, use this slightly modified expression:
^[^/?*:;{}\\]*\.?[^/?*:;{}\\]+$
Solution 4
Below the regex using for checking Unix filename in a Golang program :
reg := regexp.MustCompile("^/[[:print:]]+(/[[:print:]]+)*$")
OrangeRind
What about me? Nothing really. Regular guy. Bit of brain. Lots of interests.
Updated on September 09, 2020Comments
-
OrangeRind over 3 years
I want to check whether a string is a file name (name DOT ext) or not.
Name of file cannot contain
/ ? * : ; { } \
Could you please suggest me the regex expression to use in preg_match()?
-
Gumbo almost 15 yearsConsider that
foobar
or.htaccess
is a valid filename too. -
quantum over 11 yearsWindows or *nix? POSIX standard allows everything except
NUL
and/
. -
Richard de Wit over 10 yearsAlso, Windows files can't contain
| " < >
and can contain{ }
-
-
Tomalak almost 15 yearsYou can of course you something other than "/" to delimit the regex in preg_match(), this removes the need to escape forward slashes specifically, but adds the need to escape the new delimiter character (if you want to use it in the regex).
-
hunt almost 15 years@Richie, do you really need the second backslash just before the dot separator? shouldn't it be ...}\\]+\.[^/?....
-
Tomalak almost 15 years@Rob Wells: It is necessary since PHP strings use the backslash as an escape character as well. "\\" in a PHP string is translated to "\" in the regex. That "\." is translated to "\." is a coincidence resulting from the fact that "\." has no meaning to PHP and is therefore left unchanged. Nontheless it is sloppy not to escape the backslash.
-
OrangeRind almost 15 yearserror coming. might be my mistake, please see I entered $page_regex = "[[^/?*:;{}\]+\\.[^/?*:;{}\]+"; $len = $this->REQ_URI_PATH_E_LEN; //length of $this->REQ_URI_PATH_E_LEN given below $page = $this->REQ_URI_PATH_E[$len - 1]; //holds path elements like 0=>posts, 1=>hello, 2=>newp.php for localhost/posts/hello/newp.php?id=3 if(preg_match($page_regex,$page)) echo "page"; else echo "Folder"; error is "Warning: preg_match() [function.preg-match]: No ending matching delimiter ']' found in line" I might be wrong with the syntax, please correct me.
-
RichieHindle almost 15 yearsYou have two opening brackets right at the start of the string: "[[
-
OrangeRind almost 15 yearssorry probably typo but "[^/?*:;{}\]+\\.[^/?*:;{}\]+" is what i entered and it gave the error. Copied it now straight from your answer to this comment and my code as well :(
-
RichieHindle almost 15 yearsPerhaps you need to escape the forward slashes: [^\/?*:;{}\]+\\.[^\/?*:;{}\]+
-
OrangeRind almost 15 yearsno luck. but $page_regex is showing [^\/?*:;{}]+\.[^\/?*:;{}]+ on echo. gawd. this regex mumbo jumbo is getting on mah nerves
-
OrangeRind almost 15 yearseven [^/?*:;{}\]+\\.[^/?*:;{}\] doen't work. whats the + at the end for (in your answer)?
-
RichieHindle almost 15 years+ means "one or more". I suggest you write a self-contained failing example and post that, and the errors you're getting, in a new question.
-
rap-2-h almost 12 yearsWarning ! A filename like
.
or..
can be dangerous in some case, and is valid with^[^/?*:;{}\\]*\.?[^/?*:;{}\\]+$
-
rap-2-h almost 12 years
"[^/?*:;{}\\]+"
can be dangerous in some cases, cause it validates filename like.
or..
-
Gajus over 11 yearsWhy did you not include delimiters?
/^[^\/\?\*:;{}\\\]+\.[^\/\?\*:;{}\\\]+$/
-
RichieHindle over 11 years@GajusKuizinas: Because the delimiters aren't a part of the regular expression itself.
-
mikewasmike about 5 yearsto allows files without extensions use:
preg_match('=^[^/?*;:{}\\\\]+\.?[^/?*;:{}\\\\]?$=', 'file.abc');