Note: The article is from Eon to PHP video tutorial, this article is limited to exchange use, not for commercial purposes, otherwise the consequences.
Learning Essentials:
1. Regular expression syntax (Perl-style)
2. Elements in a regular expression
3.Perl style functions
When working with strings, there are many more complex strings that cannot be cleanly completed with normal string processing functions. Than
For example, it may be necessary to verify that an email address is legitimate, so you need to review many rules that are not easy to check. This is
is where the regular expression comes in. Regular expressions are powerful and concise groups of characters that can contain a large number of logical
, it is particularly noteworthy that regular expressions are fairly brief.
A Regular expression syntax (Perl-style)
Perl has always been considered one of the greatest analytic languages, providing a comprehensive regular expression, even the most
Complex string patterns can also be searched and replaced with this regular expression language. PHP developers recognize that the
Its re-inventing regular expressions, rather than letting PHP users directly use the famous Perl regular expression language, Perl
Style of function.
Pattern rule:/php/two slashes before and after a string.
The match function: the Preg_match () function searches the string for a pattern, returns true if it exists, or returns false.
< span="">('/php/', ' php '< span="">?>
Two Elements in a regular expression
The regular expression contains three elements: quantifier, metacharacters, modifier
Quantifiers
Metacharacters
Modifier
Three Perl style functions
PHP provides 7 functions for using a Perl-compatible regular expression search string, including: Preg_grep (),
Preg_match (), Preg_match_all (), Preg_auote (), Preg_replace (), Preg_replace_callback (), and
Preg_split ().
Search string: The Preg_grep () function searches all elements in the array, returning all the meta-items that match a pattern
An array of elements.
< span="">< span="">(' php ', ' asp ', ' jsp ', ' Python ', ' Ruby '< span="">< span="">(< span="">'/p$/', ?> < span=""> < span="">
Search mode: the Preg_match () function searches for a pattern in a string, returns True if it exists, otherwise returns false.
< span="">< span="">('/php[1-6]/', ' php5 '< span="">?>
Email Verification Small Case (group app)
< span="">= '/([\w\.\_]{2,10}) @ (\w{1,}). ([a-z]{2,4})/' < span=""> < span=""> = ' yc60.com@gmail.com ' < span=""> < span="">(,?>< span="">< span=""> < span=""> < span="">
All occurrences of the matching pattern: the Preg_match_all () function matches all occurrences of the pattern in the string, and then the
All the matches are put into the array.
< span="">('/php[1-6]/', ' php5sdfphp4sdflljkphp3sdlfjphp2 ', < span=""> < span=""> < span=""> (< span="">?>< span="">
Bound special Regular expression: Preg_quote () in each word that has a special meaning for regular expression syntax
Match either inserts a backslash. These special characters contain: $ ^ * () + = {} [] | \ \: <>.
< span="">< span="">(' PHP price is: $?> '< span="">
Replace all occurrences of the pattern: the Preg_replace () function searches for all matches and then replaces the desired string with the return
Back out.
< span="">< span="">('/php[1-6]/', ' python ', ' This was a php5,this is a php4 '< span="">?>
UBB Small Case: Greedy problem + Group use ()
< span="">= '/\[b\] (. *) \[\/b\]/u ' < span=""> < span=""> = '\1' < span=""> < span=""> = ' This was a [B]php5[/b],this is a [b]php4[/b] ' < span=""> < span=""> < span=""> (< span="">,,?>< span="">< span="">< span="">
Divides a string into different elements in a case-insensitive manner: Preg_split () is used to split different elements.
< span="">(< span="">'/[\.@]/', ' yc60.com@gmail.com '< span="">?>
Note: Currently, PHP uses a POSIX-style regular expression search string with 7 functions, including: Ereg (),
Ereg_replace (), eregi (), Eregi_replace (), Split (), Spliti (), and Sql_regcase ().
PS: And Perl style Basically the same, you can refer to the manual to learn by themselves.
http://www.bkjia.com/PHPjc/748763.html www.bkjia.com true http://www.bkjia.com/PHPjc/748763.html techarticle Note: The article is from Eon to PHP video tutorial, this article is limited to exchange use, not for commercial purposes, otherwise the consequences. Learning Essentials: 1. Regular expression syntax (Perl style) 2. ...