Match no aplahnumeric ignoring spaces

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

Moderator: General Moderators

Post Reply
Dedicated Kid
Forum Newbie
Posts: 3
Joined: Sat Sep 25, 2010 6:42 am

Match no aplahnumeric ignoring spaces

Post by Dedicated Kid »

hi all

i am new to regex and have been trying to write a simple expression to detect if a user has entered any non alphanumeric characters in a field, but ignoring spaces as people will use spaces obviously.

the code i have wriiten to test the expression is very simple: as below

if (preg_match('/\W/gx',"Big Brown Fox - 1234567890",$matchlist)){

echo "not clear";

}else{

echo "clear";

}


print_r($matchlist);

now when i run this without the "gx" on the end it works but picks up spaces, but with the "gx" included it comes back as non found or "clear".

can anyone tell me what i am doing wrong?

thanks in advance.
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Match no aplahnumeric ignoring spaces

Post by ridgerunner »

You wish to match characters that: "Match no alphanumeric ignoring spaces". Easy! Just use a negated character class that matches anything other than what is in the class. You specify a negated char class by placing a ^ caret at the very beginning of the class like so:

Code: Select all

if (preg_match('/[^A-Za-z0-9\s]/',"Big Brown Fox - 1234567890",$matchlist)) {
    echo "not clear";
} else {
    echo "clear";
}
Also, it is apparent that you don't understand the meaning of the modifiers you have applied to your regex. The "x" modifier is for free-spacing mode, which allows commenting a regex, and the "g" modifier is not even mentioned in the PHP documentation (See: PHP Pattern Modifiers). Neither of these modifiers have anything to do with your problem at hand.

Regexes are like a loaded weapon - very powerful, but you need to be very careful (or you might shoot yourself in the foot!). I would recommend taking the time to learn the basics. It doesn't take long and there are excellent free online resources. You could start with the tutorial here: Regular-Expressions.info. I can guarantee that the time spent will pay for itself many times over!

Cheers!
Dedicated Kid
Forum Newbie
Posts: 3
Joined: Sat Sep 25, 2010 6:42 am

Re: Match no aplahnumeric ignoring spaces

Post by Dedicated Kid »

Thanks ridgerunner. your absolutely right regex is very powerful and i do need to learn, i have been studying for a couple of days now but cant seem to find a resource that deals with only php.

It is i think very complicated or i am making it so one or the other, there are so many flavors and variations.

I will be implementing a search engine on a web site in the coming weeks and i have a feeling that regex will be worth its wait in gold (that is a metaphor as regex is a digital phenomenon and has no mass but you know what i mean). thanks for your help no doubt i will be posting again soon in relation to regular expressions.
Post Reply