View Full Version : simple regex

11-02-2007, 03:55 PM
i have this code

if (preg_match("{[a-zA-z]}", $variable) && !preg_match("{[0-9]}", $variable)) {

what i want is that variable can only contain letters, and that works fine. but, how can i combine that reg ex into just one.

And one more thing. I would also like to disallow characters like ! " # $... etc.

Do i need to create something like {[!"#$%&/()=?*]} and disallow it, or is there a special way to do that.

11-02-2007, 04:14 PM
Check out http://www.php.net/ctype, namely http://www.php.net/ctype_alpha.

11-02-2007, 04:14 PM
with character groupings, you can negate the matching, so to check if a string contains nothing but a-z, case-insensitively, you can do

$pattern = '#[^a-z]#i';
if(preg_match($pattern, $string)>0) {
//there was something other than a letter

Alternatively, ditch the regexp and use the ctype_alpha() function, that given a string returns false if there are any non-alphabetic characters in it.

11-02-2007, 04:37 PM
Thanks for the ctype info, but i would like to learn regex.
@GJay -> your regex isn't working right..

i tried this if (preg_match("{#[^a-z]#i}", $variable)) { and it's never true :confused:
i checked out a bit my regex literature and i couldn't find # anywhere. btw, i like to use perl regex not posix if that means anything in this case.

thanks anyway...

11-02-2007, 05:05 PM
The hash symbol (#) is simply a delimiter.

In PHP's patterns, you always start with a delimiter (the first character in the pattern) and end with the same. Then follow it with any modifiers. These all work the same:


It's whatever you find easier to read.

Rather than solve this relatively simple problem, I'll give a design tip.

Instead of using this:

if (preg_match("{#[^a-z]#i}", $variable)) {

Why not create this:

function checkAlpha($value) {
return preg_match("{#[^a-z]#i}", $value);

Now you have a nifty little function that solves your problem. And you won't have to rewrite it anywhere. If you put it in a "library" file, you can re-use it for other sites.

11-02-2007, 05:21 PM
thanks for the design tip, but obviusly this does nothing..
if (preg_match("{#[^a-z]#i}", $variable)) {
i know that ^ means begins with, and i means case insensitive, but that match doesn't match anything. why can't someone just write working example. is it my karma or what?

11-02-2007, 05:35 PM
It's the "{}" you put into the regex string. preg_* functions require you to put the regex petween two same characters (a delimiter), so it can distinguish between the actual regular expression and the flags you set (i in you case).

Apparently "{}" can also be used as a pair of delimiters (as you would get an error if there would be no closing delimiter), which means that the function will attempt to match the following:
The # character,
Any character that is not a lower-case letter,
The # character
The 'i' character.
Using: "#[^a-z]#i" will only match any character which is not a lower-case letter.

11-02-2007, 06:26 PM
i used to much delimiters. i thought that even 'i' needs to be inside delimiters. i was wrong. Thanks GJay for providing solution in first post, and sorry for me not seeing the obvious. :thumbsup:
tnx marek for undeleting the last post ;) i think i'm ready for next regex step (validating email :lol) so stay tuned it's gonna be a lota fun... :D

11-02-2007, 06:40 PM
Using: "#[^a-z]#i" will only match any character which is not a lower-case letter.

A HA!!

this has totaly different meaning!

#^[a-z]#i than this #[^a-z]#i

in first case it matches the first characters!! in second it means everything but the characters inside!!! :D

i'm going to buy few bananas couse i feel like monkey :-(l)

11-02-2007, 07:21 PM
but obviusly this does nothing..
if (preg_match("{#[^a-z]#i}", $variable)) {

Correct. I was merely quoting your regular expression. :)

11-02-2007, 07:40 PM
quoting or mocking :p

and here's for the e-mail


i really wonder will this work at all :D

11-02-2007, 08:10 PM
Figured it out. This is for now what i'm gonna use, just i have one question..
here's regy "{[a-z]@[a-z][^.][a-z]{3-4}}" - well that is not so complicated, but i was wondering on why do i need [^.]. Why didn't it work with something this simple "{[a-z]@[a-z]\.[a-z]{3-4}}" or even "{[a-z]@[a-z][\.][a-z]{3-4}}" . for don't know what reason i used [^.], and it works now. i'll try to figure that one out by myself, but sure could use your help.