note 26116 added to ref.regex
| From: | moc at rack1 dot php dot net | Date: | Fri, 18 Oct 2002 14:28:23 +0000 |
| Subject: | note 26116 added to ref.regex | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-38252@lists.php.net to get a copy of this message | ||
In a PCRE \s matches whitespace, but not inside a character class:
preg_match ('/\s/', ' ') // match
preg_match ('/[\s]/', ' ') // no match
Within a character class [:space:] is treated as a single character that matches any single
whitespace character:
$pattern = '/[[:space:]]/';
$subject = "space tab\tnewline\n";
preg_match_all($pattern, $subject, $out) // == 3
To match a hyphen from within a character class, it must either be first or last; otherwise, it will
act as a range operator.
Example: To match a blank string or a string containing only uppercase letters, underscores, spaces,
and hyphens:
preg_match('/^[A-Z_ -]*$/', $subject)
To match any whitespace, not just spaces:
preg_match('/^[A-Z_[:space:]-]*$/', $subject)
--
http://www.php.net/manual/en/ref.regex.php
http://master.php.net/manage/user-notes.php?action=edit+26116
http://master.php.net/manage/user-notes.php?action=delete+26116
http://master.php.net/manage/user-notes.php?action=reject+26116