In the process of development, we often use regular expressions, for beginners, many times know how the regular expression is, but when the real need to use, but do not know what function, the specific modifier is also more chaotic. The following small series for you to organize some PHP regular expressions commonly used in the functions and related elements.
Regular functions in 1.PHP
In PHP programming, the usual regular functions are mainly a.preg_match and b.ereg regular function two, wherein:
A.preg_match in the Perl language
Syntax: Preg_match (mode,string Subject,array matches)
Description: The mode parameter----regular module, which is the regular expression (syntax)
The contents of the subject parameter----regular
Matches parameter----The result of a regular (get the form of an array)
B.ereg regular function to POSIX basis (Unix, Script)
Syntax: Ereg (mode, string subject, array regs)
2. Elements in a regular expression
A. Atoms (ordinary characters: A-Z-A-0-9, atomic tables, escape characters)
B. metacharacters (characters with special features, such as: #, *)
C. Pattern modifier (System built-in part characters I, M, S, U ... )
3. "Atoms" in regular expressions
A.a-z A-Z _ 0-9//most common characters
B. (ABC) (SKD)//cell symbol enclosed in parentheses (one whole)
C.[ABCS][^ABD]//The atomic table enclosed in square brackets, the ^ in the atomic table represents the excluded or opposite content
D. Escape character
\d contains all the numbers [0-9]
\d except all numbers [^0-9]
\w includes all English characters [A-Z A-Z 0-9]
\w except for all English characters [^a-z A-Z 0-9]
\s return, line change, etc.
......
Note: The parentheses must be whole to match; The square brackets can match as long as they are subsets (content exists)
4. Regular expression meta-characters
* Match the previous content 0 times 1 or more times
. Matches content 0 times 1 times or more, but does not include carriage returns (substituting for yourself, anything)
+ match 1 or more times of previous content
? Match the previous content 0 or 1 times
| Select match like in PHP | (because this operation conforms to the weak type leading to the most overall match, similar to a word match)
^ Match String header contents
$ match String Trailing content
\b Matches a word boundary, which can be either a space or a special match (with a word delimiter, like a space)
\b Match exception with word boundary unexpected content (no word delimiter)
{m} matches the previous content with a repeat number of M times
{m,} The number of repetitions of the previous content is greater than or equal to M times
{M,n} matches the number of repetitions of the previous content m times to N times
() merge the whole match, and put in memory, can use \\1 \\2 ... Get (call to put in-memory content)
5. Sequence of operations
Still follow the arithmetic rules from left to right
Priority level:
() parentheses because it is memory processing, so the highest
* ? + {} Repeat match content next
^ $ \b Border Processing third
| Condition Processing IV
Finally, the matching is calculated in order of operation
6. Mode modifier
A pattern modifier is a feature that is enhanced and supplemented for PHP regular expressions and is used outside of regular, such as:/Regular/U
Common modifiers:
I regular content is not case-sensitive at match time (by default it is differentiated)
M uses multi-line recognition matching when matching first content or tail content
S will escape carriage return cancellation is for a single line match as. Match the time
x ignores whitespace in the regular
A Force A match from the beginning
D force $ match tail without any content \ n
U disallow greedy match to trace only to the nearest match and end, regular expression commonly used on the collector
7. Matching function
Preg_match_all All matching functions
Syntax: Preg_match_all (string pattern, string subject, array matches [, int flags])
Description: Sort the results to make $matches [0] match the number of all patterns
Purpose: Intercept more detailed content, collect Web pages, analyze text
8. Replacement function
Preg_replace Regular Replacement function
Syntax: preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])
Description: Replace the relevant content with a regular expression, similar to the previously learned str_replace string substitution, but the function is stronger than it
Hint: 1, the replacement content can be a regular or an array of regular
2, the replacement content can be replaced by the modifier e to resolve the content of the execution
Purpose: Replace some of the more complex content, also can be used for the conversion of content
9. Split function
Preg_split Regular Cutting
Syntax: Preg_split (string pattern, string subject [, int limit [, int flags]])
Description: Cutting related content with regular expressions, similar to the explode cutting function previously learned, but explode can only be cut in one way with limitations.
The above is the PHP development of regular expressions commonly used functions, elements and related use, I hope that everyone in the writing of regular expressions help, is not very clear to the pro, you can use these things described above to try to write one or two regular expressions run.