Page 1 of 1

Regex for controlling non-letter/number charcters

Posted: Fri Feb 11, 2005 9:46 am
by neophyte
I can't seem to find directions for controlling non-letter/number characters($#&@^!*()><?":{}) in regular expressions. Can someone enlighten me.

Example

A preg match expression that tests true for numbers only no special charcters no letters

/[^a-zA-Z]/

That locks out letters, how do you exclude the other characters?

Posted: Fri Feb 11, 2005 10:53 am
by feyd

Code: Select all

$only_numbers = (bool)preg_match( '#^&#1111;0-9]+$#', $string );
is_numeric() can be used as well..

Okay

Posted: Fri Feb 11, 2005 11:37 am
by neophyte
I get it. Once a noob always a noob. :oops:

Okay so the difference is you added the marks for begin of string "^" followed by the character class definition and end of string "$". With out the beginning and end the string will only test if the string has numbers.

Alright I think I'm getting the hang of this... :roll:

Thanks Feyd! :wink:

Posted: Fri Feb 11, 2005 12:28 pm
by timvw
a book i can strongly recommend you:

Mastering Regular Expressions
Powerful Techniques for Perl and Other Tools
Jeffrey E.F. Friedl
O'REILLY

Posted: Fri Feb 11, 2005 12:32 pm
by feyd
yes.. a wonderful book I have recommended many times. Anything O'Reilly, I usually say. :)

Posted: Fri Feb 11, 2005 1:50 pm
by neophyte
Thanks for the tip guys... I'll have to check out that book. Thanks again