pattern match for a folder

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

pattern match for a folder

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Moved to regex
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: pattern match for a folder

Post 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

;)
Post Reply