Page 1 of 1

pattern match for a folder

Posted: Tue Jul 05, 2005 1:54 pm
by andylyon87
I am lookin for a regular expression I can use for folder names.

Users enter a folder name into a form, this name then needs to be validated. I dont seem to be able to get this to work:

Code: Select all

$pattern = "[[]]_-+";
	if(!ereg($pattern, $new_cat)){
		print("The category chosen has some invalid characters in the name please retype it without \"';:\/><+=)(*&^%\$£!¬.\" ");
		$i++;
	}
The only characters I want to allow are - and _ but I dont mind how many anyone uses.
Thx andy

Posted: Tue Jul 05, 2005 4:08 pm
by Chris Corbyn
Moved to regex

Re: pattern match for a folder

Posted: Tue Jul 05, 2005 4:15 pm
by Chris Corbyn
andylyon87 wrote:The only characters I want to allow are - and _ but I dont mind how many anyone uses.
Do you mean, any letter, number, underscore or hyphen?

Code: Select all

preg_match('/^[\w\-]+$/', $string);
But there's other characters allowed in directory names (especially if the path is to be given too).

Explanation of pattern (I think I need to start doing this):
Caret "^" --> Start of string
[ ] --> Character class. Only allow characters inside these brackets
\w --> Any letter, number, or underscore
\- --> Hyphen (I just escaped it with a backslash).
[ ]+ --> Character class applies to one or more characters.
Dollar "$" --> End of string

;)