views:

1357

answers:

3

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()?

+2  A: 

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: "[^/?*:;{}\\]+"

RichieHindle
@Richie, do you really need the second backslash just before the dot separator? shouldn't it be ...}\\]+\.[^/?....
Rob Wells
@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.
Tomalak
error coming. might be my mistake, please seeI 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.
OrangeRind
You have two opening brackets right at the start of the string: "[[
RichieHindle
sorry probably typobut "[^/?*:;{}\\]+\\.[^/?*:;{}\\]+" is what i entered and it gave the error. Copied it now straight from your answer to this comment and my code as well:(
OrangeRind
Perhaps you need to escape the forward slashes: [^\/?*:;{}\\]+\\.[^\/?*:;{}\\]+
RichieHindle
no luck. but $page_regex is showing [^\/?*:;{}\]+\.[^\/?*:;{}\]+ on echo.gawd. this regex mumbo jumbo is getting on mah nerves
OrangeRind
even [^/?*:;{}\\]+\\.[^/?*:;{}\\] doen't work.whats the + at the end for (in your answer)?
OrangeRind
+ 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.
RichieHindle
+1  A: 

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:

^[^/?*:;{}\\]*\.?[^/?*:;{}\\]+$
Tomalak
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).
Tomalak
+1  A: 
$a = preg_match('=^[^/?*;:{}\\\\]+\.[^/?*;:{}\\\\]+$=', 'file.abc');

^ ... $ - begin and end of the string
[^ ... ] - matches NOT the listed chars.
hegemon