What elements are commonly used regular expressions in PHP? In program development, we often use regular expressions. for beginners, we often know what the regular expressions are, but when we really need to use them, we don't know what functions to use, the modifier is also confusing. The following small series will help you sort out some common functions and related elements in php regular expressions.
1. Regular functions in PHP
In php programming, common regular functions include a. preg_match and B. ereg:
A. preg_match in 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 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 php regular expressions. it is used outside of 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.
The above is a common function, element, and usage method used to develop regular expressions in PHP. I hope it will be helpful for you to compile regular expressions, you can use the above mentioned things to try to write one or two regular expressions.