This article mainly introduces the PHP Regular Expression summary. For more information, see
This article mainly introduces the PHP Regular Expression summary. For more information, see
1. Two common regular functions in PHP
A. preg_match regular function, based on perl
Syntax: preg_match (mode, string subject, array matches)
Note: The mode parameter ---- the regular expression module, that is, the regular expression (syntax)
Subject parameter ---- regular content
Matches parameter ---- regular result (get an array form)
B. ereg regular function, based on POSIX (Unix, Script)
Syntax: ereg (mode, string subject, array regs)
2. elements included in regular expressions
A. Atom (common character: a-z A-Z 0-9, atomic table, escape character)
B. metacharacters (special features such as: # And :#,*)
C. Pattern modifier (some built-in characters I, m, S, U ...)
3. The "atom" in the Regular Expression"
A. a-z A-Z _ 0-9 // The most common character
B. (abc) (skd) // unit symbol enclosed in parentheses (a whole)
C. [abcs] [^ abd] // an atomic table in square brackets. The ^ in the atomic table indicates the excluded or opposite content.
D. escape characters
\ D contains all numbers [0-9]
\ D except all numbers [^ 0-9]
\ W contains all English characters [a-z A-Z 0-9]
\ W all English characters apart [^ a-z A-Z 0-9]
\ S carriage return, line feed, etc.
......
Note: parentheses must be integral to match; square brackets can match (content exists) as long as they are subsets)
4. Regular Expression metacharacters
* Match the previous content 0 times 1 time or multiple times
. Match content 0 times 1 time or multiple times, but does not contain carriage return line feed (refer to yourself, any content)
+ Match the previous content once or multiple times
? Matches the previous content 0 times or 1 time
| Select matching similar to that in PHP | (because this operation matches a weak type, it leads to the most overall match, similar to a word match)
^ Match the first part of a string
$ Match string tail content
\ B matches the word boundary. The boundary can be a space or a special match (with a word boundary, similar to a space)
\ B match the unexpected content except the word boundary (no word boundary)
{M} matches the previous content with M duplicates.
{M,} matches the repeat of the previous content more than or equal to M
{M, n} matches the number of repetitions of the previous content M to N
() Merge the overall match and put it into the memory. You can use \ 1 \ 2... Obtain (call the content in the memory) in sequence)
5. Operation Sequence
Still follow the operation rules from left to right
Priority:
() Parentheses are the highest for memory processing.
*? + {} Duplicate Matching content followed
^ $ \ B boundary processing third
| Condition processing fourth
Finally, matching is calculated in the order of operation.
6. Pattern Modifier
The pattern modifier is a function that is enhanced and supplemented for regular expressions. It is used outside the regular expression, for example,/regular/U.
Common modifiers:
I. The regular content is case-insensitive during matching (the default value is case-sensitive)
M uses multiple lines to identify and match the first or last content
S cancels the escape carriage return to match a single row, such as. Match.
X ignore the blank in the Regular Expression
A force match from scratch
D Force $ NO content at the end of the match \ n
U prohibit greedy match to only trace the latest match and end. It is commonly used in the regular expression of the collection program.
7. Matching
Preg_match_all: All matching functions
Syntax: preg_match_all (string pattern, string subject, array matches [, int flags])
Note: sort the results to make $ matches [0] The number of all pattern matches.
Purpose: capture detailed content, collect webpages, and analyze texts.
8. Replacement
Preg_replace Regular Expression replacement function
Syntax: preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])
Note: The regular expression is used to replace related content, similar to the str_replace string that has been learned before, but the function is better than the regular expression.
Tip: 1. The replacement content can be a regular or array regular
2. The replacement content can be replaced by the modifier e.
Purpose: replace some complicated content and use it for content conversion.
9. Segmentation
Preg_split regular cut
Syntax: preg_split (string pattern, string subject [, int limit [, int flags])
Note: regular expressions are used to cut related content. This is similar to the explode cut function that we have learned before. However, explode can only be used to cut data in one way.