note 54544 added to function.preg-match

From: Date: Thu, 07 Jul 2005 12:49:23 +0000
Subject: note 54544 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-91595@lists.php.net to get a copy of this message
LITTLE NOTE ON PATTERN FOR NOOBIE : int preg_match ( string pattern, string subject) "/^[a-z0-9 ]*$/" / =begin/end pattern ^=matching from exactly beginning of subject $=matching from exactly end of subject []=match with any character in the "[]" brackets. [a-z0-9 ]=match with any character between a to z OR 0 to 9 OR "space" (there is space between 9 and ]) *=the number of matching character in the subject can be 0 or more. Actually "[]"brackets only match for 1 character position so if you to match 1 or more use "+" match 0 or more use "*" match 5 characters use {5} match 5 to 6 characters use {5,6} on the "*" position in the example. <? preg_match ($pattern, $subject); ?> $pattern="/^[a-z0-9 ]*$/"; $subject="abcdefgadfafda65" ->TRUE $subject="abcdefg ad f afda 65" ->TRUE $pattern="/[a-z0-9 ]*/"; $subject="$$abcdefgadfafda65><" ->TRUE why? because there's no "^" on the beginning and no "$" on the end of $pattern. So the regex matchs entire $subject and found the correct one on the middle, it is OK because there's no "^" and "$" boundary. If you put only one either "^" nor "$" the regex will matchs for head nor tail of $subject only. LITTLE MORE ADVANCE checking file name string that must contain 3 characters extension ".xxx" and the file name contains alphabetic character only. here is the pattern "/^[a-zA-Z0-9]*\.[a-zA-Z0-9]{3}$/" \.=there is "." character for extension separation. Constant character must be preceeded by an "\". {3}= 3 characters extension OTHER SPECIAL CHARACTERS. I haven't examined them. .=wild card for any character |=OR ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54544 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54544&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54544&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54544&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54544&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54544&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54544&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54544&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91595) next »