A regular expression in life:
A) notepad++, word, such as the editing function of the software, has a find, replace the function, which is in fact a regular pattern of a matching, replacement, including Windows can be implemented in the search, but also the application of regular pattern matching; Matching software is also used by regular mode rules to write!
Second, the regular in PHP:
A) Definition: it is used to find, replace, match, and divide a string by using a pattern.
Third, the regular mode:
A) is actually a special string! We can use this special string to perform a series of operations on another string!
b) Mode:
I. Single mode: Find a girlfriend, learn, enrich yourself!
II. Love mode: At the moment Hao and his girlfriend are in a hot phase, when their IQ is 0
Iii. Marriage Model: Now Hao and his girlfriend married, and then live a happy life!
Iv. detection mode: At this time his daughter-in-law IQ is second only to Einstein, she will be from the clues of Hao found that the waves are not stealing fishy ~ ~ (hair silk, body whether there is perfume, whether there is lipstick ...) )
Iv. classification of the regular:
A) POSIX rules:
I. Deprecated, (function, frequency of use) is relatively backward, so deprecated
b) Pcre rules:
I. In use!
V. How to use regular expressions?
A) Regular delimiter:
b) Atom:
c) Metacharacters:
d) mode modifier:
e) Use with regular function!
I.preg_match () match once
Ii. Preg_match_all () matches all
Six, delimiter:
A) As with the definition string, we define the regular and we need to use regular delimiters
b)"/ Regular mode /" , the delimiter in the regular can be anything but a letter, a number, a special meaning symbol, can be used as a delimiter
c) Note: Normally, we will use the double slash as the delimiter of the regular pattern, which is an unwritten rule!
Atoms: The smallest unit in the regular pattern!
A) ordinary characters:
1.a-z: All lowercase letters in it are an atom
2.A-Z: All uppercase letters in it are an atom
3.0-9: All Arabic numerals, all of which belong to an atom
b) Atomic table:
1.[]: A relationship between all the contents of a square bracket is a relationship, and an atomic table always represents an atom!
2.[ABCDFG]: Single atom can be written
3.[A-Z]: Any one lowercase letter
4.[A-Z]: any one capital letter
5.[0-9]: any one Arabic numerals
6.[A-G]: Any one lowercase letter from a-g
7. If there is a '-' in the atomic table, it represents a range!
c) Special escape characters:
1.\d: Equivalent to [0-9], representing any Arabic numerals
2.\d: Represents any of the one by one elements except Arabic numerals
3.\w: Represents any letter, number, underline content
4.\w: Represents the addition of arbitrary letters, numbers, underlined content
d) non-printable characters:
1.\r: Enter
2.\n: Line break
3.\t: Tab
e) characters with special meanings in the regular:
1.* 、.、 + 、?、 \, when we want to use regular mode to match these characters with special meanings, we need to add the escape character to the front of it \
Eight, metacharacters: in regular expressions, those characters that have special meanings are meta-characters
a) []: Atomic table, characteristic: the relationship between atoms in an atomic table is or, an atomic table represents an atom
b) {n}: Number of matches: the atoms before the curly braces must appear n times
c) {n,}: Number of matches: the atoms before the curly braces appear at least n times, with no upper limit
d) {N,m}: Number of matches: the atoms before the curly braces appear at least n times, with a maximum of M times
e).: Represents any Atom
f) *: Number of matches: The atom preceding the * is any length, equivalent to {0,}
g) +: Number of matches: the Atom before the + sign is at least once, no upper limit, equivalent to {1,}
h)?: Number of matches: the atom before the symbol is optional, equivalent to {0,1}
i) ^: two levels of meaning
I. If it is placed after the left delimiter in the regular mode, it represents the beginning of the atom after ^
II. If you place it at the beginning of the atomic table, [^0-9], the content in the atomic table is reversed
J) $: If it is placed before the right delimiter in the regular mode, it represents the end of the atom before the $ symbol
I. Note: If mates use the UP arrow ^ and dollar sign $, then the exact match, representing the words we match, mnemonic must conform to the contents of their middle regular pattern!
k) (): three-layer meaning:
I. Priority enhancement
Ii. Sub-storage
III. Reusing mode units
L) |: or
IX. Special application of the regular pattern:
A) greedy match
1 //Define word mnemonic2 $stu= ' #仉浩焱 # #许超 # #张舒 # #大嘴 # #小花 # #伟哥 # ';3 4 //Define a regular pattern (this is a greedy match)5 $patt= "/#.*#/";6 Var_dump($res);7 8 //Results9 Array(size=1)Ten0 = One Array(size=1) A0 =string' #仉浩焱 # #许超 # #张舒 # #大嘴 # #小花 # #伟哥 # ' (length=51) - - //define regular mode (reject greedy match) the $patt= "/#.*?#/"; - - //use regular functions to match - Preg_match_all($patt,$stu,$res); + Var_dump($res); - + //Results A Array(size=1) at0 = - Array(size=6) -0 =string' #仉浩焱 # ' (length=11) -1 =string' #许超 # ' (length=8) -2 =string' #张舒 # ' (length=8) -3 =string' #大嘴 # ' (length=8) in4 =string' #小花 # ' (length=8) -5 =string' #伟哥 # ' (length=8)
b) Sub-storage
c) Reusing mode units
Functions in the Regular:
A) Preg_match (), perform a regular expression match
1 //Define word mnemonic2 $str= "ABCDEFGABCDEFG";3 4 //defining the regular pattern5 $patt= "/a/";6 7 Echo Preg_match($patt,$str,$res);8 Var_dump($res); 9 //Array (size=1)Ten //0 = String ' A ' (length=1)
b) Preg_match_all (), perform a global regular expression match
1 //Define word mnemonic2 $str= "ABCDEFGABCDEFG";3 4 //defining the regular pattern5 $patt= "/a/";6 7 //Match All8 Preg_match_all($patt,$str,$res);9 Var_dump($res);Ten One //Array (size=1) A0 = - Array(size=2) -0 =string' A ' (length=1) the1 =string' A ' (length=1)
c) Preg_grep (); Returns an array entry for the matching pattern
d) preg_replace (); Perform a search and replace of a regular expression
e) preg_split (); separating strings with a regular expression
The pattern modifier in Xi.:
A) I: Case insensitive
1 //pattern modifier I: Indicates a regular match is case insensitive2 $str= "ABCDEFGABCDEFG";3 $patt= "/[a-z]/i";4 Preg_match_all($patt,$str,$res);5 Var_dump($res);6 7 //Results8 Array(size=1)90 =Ten Array(size=14) One0 =string' A ' (length=1) A1 =string' B ' (length=1) -2 =string' C ' (length=1) -3 =string' d ' (length=1) the4 =string' E ' (length=1) -5 =string' F ' (length=1) -6 =string' G ' (length=1) -7 =string' A ' (length=1) +8 =string' B ' (length=1) -9 =string' C ' (length=1) +Ten =string' D ' (length=1) AOne by one =string' E ' (length=1) atThestring' F ' (length=1) -+ =string' G ' (length=1)
b) S: Indicates ignore line break
//pattern modifier s: Indicates regular match ignores line break$str= "<li> Zhang San </li> <li> John Doe </li> <li> Harry </li>";//do not add mode modifier s$patt= "/<li>.*?<\/li>/";Preg_match_all($patt,$str,$res);Var_dump($res);//ResultsArray(size=1) 0 =Array(size=2) 0 =string' <li> Zhang San </li> ' (length=15) 1 =string' <li> Harry </li> ' (length=15)//Add mode modifier s$patt= "/<LI>.*?<\/LI>/S";Preg_match_all($patt,$str,$res);Var_dump($res);//ResultsArray(size=1) 0 =Array(size=3) 0 =string' <li> Zhang San </li> ' (length=15) 1 =string‘<li> John Doe </li>' (length=20) 2 =string' <li> Harry </li> ' (length=15)
c) U: Indicates rejection of greedy match
1 //pattern modifier u: Indicates a regular reject greedy match2 $str= "#海波 # #鸿泽 # #柏岩 # #景飞 #";3 $patt= "/#.*#/u";4 Preg_match_all($patt,$str,$res);5 Var_dump($res);6 7 //Results8 Array(size=1)90 =Ten Array(size=4) One0 =string' #海波 # ' (length=8) A1 =string' #鸿泽 # ' (length=8) -2 =string' #柏岩 # ' (length=8) -3 =string' #景飞 # ' (length=8)
12. About regular and String functions:
A) The regular function is equivalent to a cannon:
b) The string function is equivalent to a bow and arrow:
"PHP" Regular expressions