Regex for finding valid filename

23,791

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:]]+)*$")
Share:
23,791
OrangeRind
Author by

OrangeRind

What about me? Nothing really. Regular guy. Bit of brain. Lots of interests.

Updated on September 09, 2020

Comments

  • OrangeRind
    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
      Gumbo almost 15 years
      Consider that foobar or .htaccess is a valid filename too.
    • quantum
      quantum over 11 years
      Windows or *nix? POSIX standard allows everything except NUL and /.
    • Richard de Wit
      Richard de Wit over 10 years
      Also, Windows files can't contain | " < > and can contain { }
  • Tomalak
    Tomalak almost 15 years
    You 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
    hunt almost 15 years
    @Richie, do you really need the second backslash just before the dot separator? shouldn't it be ...}\\]+\.[^/?....
  • Tomalak
    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
    OrangeRind almost 15 years
    error 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
    RichieHindle almost 15 years
    You have two opening brackets right at the start of the string: "[[
  • OrangeRind
    OrangeRind almost 15 years
    sorry 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
    RichieHindle almost 15 years
    Perhaps you need to escape the forward slashes: [^\/?*:;{}\]+\\.[^\/?*:;{}\]+
  • OrangeRind
    OrangeRind almost 15 years
    no luck. but $page_regex is showing [^\/?*:;{}]+\.[^\/?*:;{}]+ on echo. gawd. this regex mumbo jumbo is getting on mah nerves
  • OrangeRind
    OrangeRind almost 15 years
    even [^/?*:;{}\]+\\.[^/?*:;{}\] doen't work. whats the + at the end for (in your answer)?
  • RichieHindle
    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
    rap-2-h almost 12 years
    Warning ! A filename like . or .. can be dangerous in some case, and is valid with ^[^/?*:;{}\\]*\.?[^/?*:;{}\\]+$
  • rap-2-h
    rap-2-h almost 12 years
    "[^/?*:;{}\\]+" can be dangerous in some cases, cause it validates filename like . or ..
  • Gajus
    Gajus over 11 years
    Why did you not include delimiters? /^[^\/\?\*:;{}\\\]+\.[^\/\?\*:;{}\\\]+$/
  • RichieHindle
    RichieHindle over 11 years
    @GajusKuizinas: Because the delimiters aren't a part of the regular expression itself.
  • mikewasmike
    mikewasmike about 5 years
    to allows files without extensions use: preg_match('=^[^/?*;:{}\\\\]+\.?[^/?*;:{}\\\\]?$=', 'file.abc');