note 54544 deleted from function.preg-match by colder
| From: | colder@php.net | Date: | Mon, 18 Jun 2007 18:25:08 +0000 |
| Subject: | note 54544 deleted from function.preg-match by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127609@lists.php.net to get a copy of this message | ||
Note Submitter: masterkumon at yahoo dot com
----
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