This chapter mainly studies the basic grammar of regular expressions;
A regular expression is a matching pattern, and the regular expression itself is a string (with some grammatical rules and special symbols)
Regular expressions This string must be used in the corresponding function to make sense (split, replace function combined)
1. Delimiter//
2, the smallest atom of a matching unit (placed in//, in the regular expression, at least one.) Example: $reg = '/7/')
3, meta-character OFDM character is not used alone, used to modify the meta-sub, used to extend the atomic function and limit words to do supplementary instructions (for example: $reg = '/{5}/')
4, Mode modifier correction, the mode (regular) correction (written on the outside of the delimiter, written on the right side for example: $reg = '/{5}/I ')
I. Defining symbols
A delimiter represents the beginning and end of a regular expression.
Example:/[0-9]/| #[0-9]# | {[0-9]} Common wording suggested using slash;//$pattern = '/[0-9]/'
Second, the Atom
Visible Atoms:
1, punctuation; "_?" Wait a minute
2, English alphanumeric A-Z, A-Z, 0-9
3, Chinese, Japanese, Arabic and other text
4, Physics company symbol
5. Other visible characters
Invisible atoms:
1. newline characters \ n
2, enter \ r
3. tab \ t
4, Space
5. Other Unseen symbols
(All the numbers, all the words, all the blanks, all the letters, the special symbols are as follows)
. Match any character except line break
\d (all numbers) represents any number [0-9]
\d (representing all non-numbers) represents any one non-number [^0-9]
\w represents any word, A-Z-A-0-9 _ [a-za-z0-9_]
\w represents any non-word, A-Z-A-Z of all characters except 0-9 _ [^ a-za-z0-9_]
\s stands for blank [\ t \ \f \v]
\s represents any non-blank [^ \ t \ \f \v]
Custom Atomic Tables
[13579]
[A-za-z] Description:-from where to where
[^a-za-z] Description: ^ is besides what
Description: (. ) point, which can represent all of an atom
Three, meta characters
Metacharacters This cannot be used alone in regular expressions to modify atoms.
How to filter meta-characters that are atoms
* Used to decorate the front of the atom can appear 0 1 or more
+ Used to modify the number of occurrences of the previous Atom 1 or more times (not once)
? Used to modify the number of occurrences of the previous atom 0 or 1 times.
{m} is used to modify the previous atom to appear m times, one less time, no more
{n, m} is used to modify the number of occurrences of the preceding atom, between N and M, containing N, m
{n,} to decorate the preceding atom can appear at least n times, containing n
| Or the relationship that represents it on either side of the atom, only one appearance can be, but | The lowest priority
^ and \a indicate what must be started and must be written in front of the regular expression
$ and \z indicate what must end and must be written after the regular expression
\b Word boundaries
\b Non-word boundaries
() First function: Change the priority level
$str = ' orisaabc ';
$reg = '/or (is|a) abc/';
Second effect: Turning small atoms into large atoms
$str = ' SQLSALABCSQLDDFD ';
$reg = '/(SQL) */';
The third function: the sub-pattern, the entire expression is a large pattern, in parentheses is each independent sub-pattern
Fourth function: Reverse reference
? : Cancels sub-mode, as large mode (? : \d{4})
\1 Sub-mode
Four, mode modifier
Pattern modifier to be placed outside the delimiter, put on the right, the pattern modifier is a character is a function, can be combined to use;
For example: "/go*gle/i"; "/go*gle/ieu";
Role:
The pattern correction symbol can modify the interpretation of regular expressions, or extend the function of regular expressions;
I is case insensitive (default is case sensitive)
m fixed symbol correction regular expression can be treated as multiple lines, and each line can be satisfied (by default, one line) when using ^ or $
S is used to correct a newline symbol in a regular expression, which is a newline character and does not include a newline character without s, and does not contain a line break by default
x ignore fix regular expression to omit whitespace between multiple words
U (. *,. +) Regular expressions are more greedy,. *? and. +? Can cancel greed
Case of Greed:
$pattern = '/myistest.+123/'; $subject = ' look myistest__123123123123 '; $matche = [];p reg_match ($pattern, $subject, $ Matche); show ($matche);
The result of greed:
Array ( [0] = myistest__123123123123)
Cancel the greedy big u:
$pattern = '/myistest.+123/u '; $subject = ' look myistest__123123123123 '; $matche = [];p reg_match ($pattern, $subject, $ Matche); show ($matche);
To cancel the greedy result:
Array ( [0] = myistest__123)
Cases ignoring case: usage of I
$pattern = '/myistest.+123/ui '; $subject = ' look myistest__123123123123 '; $matche = [];p reg_match ($pattern, $subject, $matche); show ($matche);
The output is the same as the result;
Cases where whitespace characters are ignored: use of X
$pattern = '/M yi S test.+123/uix '; $subject = ' look myistest__123123123123 '; $matche = [];p reg_match ($pattern, $subject , $matche); show ($matche);
The output is the same as above, and no x is matched to the result;
Five, practical application
1, non-empty match '/.+/'
2, floating point number matching (reserved 2 bits) '/(?! 0\.00) (\d+\.\d{2}$)/'
$subject = [' 1.00 ', ' 2.345 ', ' 0.23 ', ' 234 ', ' 23.22 ', ' 0.00 ']; $matche = [];for ($i = 0; $i < count ($subject); $i + +) {
if (Preg_match ('/(?! 0\.00) (\d+\.\d{2}$)/', $subject [$i], $matche) { Show ($matche); }}
Results:
Array ( [0] = 1.00 [1] = 1.00) array ( [0] = 0.23 [1] = 0.23) array ( [0] = 23.22 [1] = 23.22)
3, mobile phone number '/^1 (3|4|7|8) \d{9}$/'
4, email address '/^ ([0-9a-za-z\\-_\\.] +) @ ([0-9a-z]+\\. [A-z] {2,3} (\\. [A-z] {2})?) $/i ' or '/^\w+ (\.\w+) *@\w+ (\.\w+) +$/'
5, Common URL address '/^ (https?:/ /)? (\w\.) +[a-za-z]+$/'
Note: study notes
PHP Regular Expression Basic syntax