views:

53

answers:

4

hi,

how do I write an expression which checks for lowcaps, dots, and without any white space in the string?

the code below so far was trying to check for lowcaps and dots (it does not work anyway!) but I don't know how to add in the expression for white spaces.

# check for matches of lowcaps or lowcaps with a dot
if (!preg_match('/([a-z0-9]|[a-z0-9\.])/', $cst_value))
{
  $error = true;
  echo ' please use lowcaps only with dot(s) and without any spacing.';
}

any ideas?

many thanks, Lau

+1  A: 

[a-z0-9.] matches a lower-case letter or a digit or a dot.
[^a-z0-9.] matches all characters that are not a lower-case letter or a digit or a dot.
So if /[^a-z0-9.]/ matches anywhere the string contains something other than lc-letter,digit or dot. If it does not match your condition is fulfilled.

if ( !preg_match('/[^a-z0-9.]/', $cst_value) ) {
  // only lower-case letters, digits or dots
}

or without digits

if ( !preg_match('/[^a-z.]/', $cst_value) ) {
  // only lower-case letters or dots
}

update: example:

foreach( array('abcdef', 'abc de', 'abc.de', 'aBcde') as $cst_value) {
  echo $cst_value, ': ';
  if ( !preg_match('/[^a-z.]/', $cst_value) ) {
    echo " ok.\n";
  }
  else {
    echo "failure\n";
  }
}

prints

abcdef:  ok.
abc de: failure
abc.de:  ok.
aBcde: failure
VolkerK
You either have to remove the ^ from the character group or the ! in the expression.
Felix Kling
@Felix Kling: Maybe the !, but certainly not the ^. Example added.
VolkerK
thanks. how if i want the input must contains a dot?? thanks
lauthiamkok
@Felix is right, you're coding in circles with that double negative. I'd go with `/^[a-z0-9.]+$/` (good) or `/[^a-z0-9.]/` (bad).
Alan Moore
@Alan: Ok, then remove the ! and switch the if/else branches if it really bothers you.
VolkerK
A: 
 /^[a-z0-9.]+$)/

Should do it. Just think about that only small letters, dots and digits are allowed. The expression will not match if any white-space is included. Btw. you don't have to escape meta-characters in a capture group.

^ and $ indicates that the whole string should only contain those characters in the capture group (they mark the start and the end of the string) and the + says that at least one of these characters must occur. Depending on your needs you can change it to e.g. {3,} which means that the string must be at least 3 characters long.

Example:

$values=array("fooBar", "123.45", "foo bar", "foo.bar");

foreach($values as $value) {
    if (!preg_match('/^[a-z0-9.]+$/', $value))
    {
         echo "Not valid: $value\n";
    }
}

prints:

Not valid: fooBar
Not valid: foo bar
Felix Kling
thanks. it works well but if I tested it with an URL with http:// at the beginning.it will print this URL in the not match's output.why is that? can I accept URL inputs?thanks
lauthiamkok
@lauthiamkok: Well, as I said, the regular expressions basically means: Accept everything that is either small latter, a digit or a dot. But `http://` contains `:` and `/` too. You would have to add those characters to the character class too, but it won't validate whether the URL is correct.
Felix Kling
A: 

I think you want this:

if ( preg_match( '/[^a-z0-9.]/' ) ) {
    $error = true;
}

Keep in mind, I removed the !. Now if this matches, you get the error. VolkerK reversed it but forgot to remove the !, I think.

Also, http://regexpal.com/ is your friend.

livingtech
A: 

'/^[a-z0-9\.]+$/' should be the right regex.

Slinky