My regular expression entry is originated from an online article [1]. This article explains the regular expression usage from a simple perspective. I think it is a good entry-level material, however, it is still dependent on the individual who will continue to forget it when using it. Therefore, I read this article four or five times repeatedly. For some of the difficult knowledge points, it may take a long time to digest, but as long as you can stick to it, you will find that your use of regular expressions will significantly improve.
Regular Expression:
A syntax rule used to describe character arrangement and matching modes. It is mainly used for string mode segmentation, matching, search and replacement operations.
Regular Functions in PHP:
Php has two sets of regular functions, which have similar functions:
One set is provided by the PCRE (Perl Compatible Regular Expression) Library. Functions with the prefix "prefix;
A set of extensions provided by POSIX (Portable Operating System Interface of Unix. Use a function named with the prefix "ereg _". (POSIX Regular Expression Library is not recommended since PHP 5.3 and will be removed from PHP6)
Since POSIX regular expressions are coming soon, And the PCRE and perl forms are similar, it is more conducive to switching between perl and php, so here we will focus on the use of PCRE regular expressions.
PCRE Regular Expression
PCRE is called Perl Compatible Regular Expression, which means Perl is Compatible with Regular expressions.
In PCRE, a pattern expression (that is, a regular expression) is usually included between two Backslash "/", such as "/apple /".
There are several important concepts in Regular Expressions: metacharacters, escaping, pattern units (duplicates), assignees, references, and assertions, these concepts can be easily understood and mastered in article [1.
Frequently Used metacharacters (Meta-character ):
Metacharacters
\ A matches the atom at the beginning of A string
\ Z matches the atoms at the end of a string
\ B matches the boundary of the word/\ bis/string with the matching header is/is \ B/string with the matching tail is/\ bis \ B/Boundary
\ B matches any character except word boundary/\ Bis/matches "is" in the word "This"
\ D matches a number, which is equivalent to [0-9].
\ D matches any character except a number. It is equivalent to [^ 0-9].
\ W matches an English letter, number, or underline. It is equivalent to [0-9a-zA-Z _]
\ W matches any character except English letters, numbers, and underscores. It is equivalent to [^ 0-9a-zA-Z _]
\ S matches a blank character; equivalent to [\ f \ t \ v]
\ S matches any character except the white space. It is equivalent to [^ \ f \ t \ v]
\ F matching a page feed is equivalent to \ x0c or \ cL
Match a line break; equivalent to \ x0a or \ cJ
Matching a carriage return is equivalent to \ x0d or \ cM.
\ T matches a tab. It is equivalent to \ x09 \ or \ cl.
\ V matches a vertical tab, which is equivalent to \ x0b or \ ck
\ ONN matches an octal number
\ XNN matches a hexadecimal number
\ CC matches a control character
Pattern Modifiers ):
Pattern delimiters are used in case-insensitive and multi-row matching. These delimiters often solve many problems.
I-matching uppercase and lowercase letters at the same time
M-treat strings as multiple rows
S-treats a string as a single line, and line breaks are treated as common characters so that "." matches any character.
The white space in X-mode is ignored.
U-match to the nearest string
E-use the replaced string as the expression
Format:/apple/I matches "apple" or "Apple", and case insensitive. /I
PCRE mode unit:
// 1. Extract the first attribute
/^ \ D {2} ([\ W]) \ d {2 }\\ 1 \ d {4} $ matches strings such as "12-31-2006", "09/27/1996", and "86 01 4321. However, the above regular expression does not match the "12/34-5678" format. This is because the result "/" of the mode "[\ W]" has been stored. When the next position "\ 1" is referenced, the matching mode is also the character "/".
When you do not need to store matching results, use the non-storage mode unit "(? :)"
For example /(? : A | B | c) (D | E | F) \ 1g/will match "aEEg ". In some regular expressions, it is necessary to use non-storage mode units. Otherwise, you need to change the subsequent reference sequence. The preceding example can also be written as/(a | B | c) (C | E | F) \ 2g /.
PCRE regular expression function:
Preg_match () and preg_match_all ()
Preg_quote ()
Preg_split ()
Preg_grep ()
Preg_replace ()
For specific functions, we can find them in the PHP manual. Below are some accumulated Regular Expressions:
Matching action attributes
Copy codeThe Code is as follows: $ str = '<form name = "adfa" action = "asdf. bphp "target =" "> <form name =" bbbb "action =" http://www.bac.com/test.php "target =" qwerqwerq "> <form name =" bbbb "action =" http. php "target =" qwerqwerq "> ';
$ Match = '';
Preg_match_all ('/\ s + action = \"(?! Http :)(.*?) \ "\ S/', $ str, $ match );
Print_r ($ match );
Use callback in Regular ExpressionsCopy codeThe Code is as follows :/**
* Replace some string by callback function
*
*/
Function callback_replace (){
$ Url = 'HTTP: // esfang.house.sina.com.cn ';
$ Str = '<form name = "adfa" action = "asdf. bphp "target =" "> <form name =" bbbb "action =" http://www.bac.com/test.php "target =" qwerqwerq "> <form name =" bbbb "action =" http. php "target =" qwerqwerq "> ';
$ Str = preg_replace ('/(? <= \ Saction = \")(?! Http :)(.*?) (? = \ "\ S)/E', 'search (\ $ url, \ 1) ', $ str );
Echo $ str;
}
Function search ($ url, $ match ){
Return $ url. '/'. $ match;
}
Regular Expression matching with assertionsCopy codeThe Code is as follows: $ match = '';
$ Str = '<a href = ""> xxxxxx.com.cn </a> <B> bold font </B> <p> paragraph text </p> ';
Preg_match_all ('/(? <= <(\ W {1})> ).*(? = <\/\ 1>)/', $ str, $ match );
Echo "<br/> match the content in HTML tags without attributes :";
Print_r ($ match );
Replace the address in the HTML source code
$ Form_html = preg_replace ('/(? <= \ Saction = \ "| \ ssrc = \" | \ shref = \")(?! Http: | javascript )(.*?) (? = \ "\ S)/e ', 'add _ url (\ $ url, \' \ 1 \ ')', $ form_html );
Finally, although the regular expression tool is powerful, in terms of efficiency and writing time, sometimes it may not be as direct as explode. For some urgent or less demanding tasks, A simple and crude method may be better.
For the execution efficiency between the preg and ereg series, I have seen the article saying that preg is a little faster, because there are not many ereg instances, and we have to launch the stage of history, I am not doing this because I am more inclined to add a person than PCRE. If you are familiar with it, you can make comments. Thank you.
References:
1. Regular Expression thirty-minute entry http://www.jb51.net/tools/zhengze.html
PHP ereg http://cn.php.net/manual/en/function.ereg.php (English version)
Http://www.jb51.net/shouce/php5/zh/function.ereg.html (Chinese)
3. PHP regular function http://www.jb51.net/article/20400.htm