matching

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

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

matching

Post by s.dot »

All of these seem to match, when they shouldn't! what am i doing wrong?

Code: Select all

if(!preg_match("#[\w]+#i","scott%")){
	die("does not match");
} ELSE {
     die("matches");
}

if(!preg_match("#\w+#i","scott%")){
	die("does not match");
} ELSE {
     die("matches");
}


if(!preg_match("#[a-z0-9_]+#i","scott%")){
	die("does not match");
} ELSE {
     die("matches");
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're missing start and end of string hooks

Code: Select all

if(!preg_match("#^[\w]+$#i","scott%")){
    die("does not match");
} ELSE {
     die("matches");
}

if(!preg_match("#^\w+$#i","scott%")){
    die("does not match");
} ELSE {
     die("matches");
}


if(!preg_match("#^[a-z0-9_]+$#i","scott%")){
    die("does not match");
} ELSE {
     die("matches");
}
Post Reply