PHP regular expressions are used primarily for string pattern segmentation, matching, lookup, and substitution operations. Using regular expressions can be inefficient in some simple environments, so how to better use PHP regular expressions requires a comprehensive consideration.
My PHP is getting started, is an article originating from the Internet, this article elaborated the PHP regular expression uses the method, I thought is a very good introductory material, but the study still relies on the individual, in the use process, still will continue to forget, therefore repeatedly read this article to have four or five times, For some of the more difficult points of knowledge, even take a long time to digest, but as long as you can see the persistence of reading, you will find yourself to the regular use of the ability will be significantly improved.
The definition of the PHP regular expression:
A syntax rule used to describe the character arrangement and matching pattern. It is mainly used for pattern segmentation, matching, lookup and substitution operations of strings.
Regular functions in PHP:
PHP has two sets of regular functions, the two functions are similar, respectively:
A set is provided by the Pcre (Perl compatible Regular Expression) library. A function named after a prefix using "preg_";
One set is provided by the POSIX (portable operating System Interface of Unix) extension. Use a function named "Ereg_" (the POSIX regular function library, which is not recommended after PHP 5.3, will be removed after PHP6)
Because POSIX is about to launch the history stage, and Pcre and Perl form the same, more conducive to our transition between Perl and PHP, so here focus on pcre regular use.
Pcre Regular Expression
Pcre is all called Perl compatible Regular Expression, meaning Perl-compatible regular expressions.
In Pcre, a pattern expression (that is, a regular expression) is typically included between two backslashes "/", such as "/apple/".
Some of the important concepts in the regular are: metacharacters, escapes, pattern units (repetition), antisense, references, and assertions, which can be easily understood and mastered in article [1].
Commonly used metacharacters (Meta-character):
Metacharacters description
\a an atom that matches the first string of strings
\z the atoms that match the tail of string strings
\b Matches the bounds of the word/\bis/the string that matches the header to the is/is\b/the string that matches the end is the/\bis\b/delimitation
\b matches any character except the word boundary/\bis/matches the "is" in the word "this"
\d match a number; equivalent to [0-9]
\d matches any character except a number; equivalent to [^0-9]
\w match an English letter, number, or underscore; equivalent to [0-9a-za-z_]
\w matches any character except English letters, numbers, and underscores; equivalent to [^0-9a-za-z_]
\s match a blank character; equivalent to [\f\t\v]
\s matches any one character except whitespace; equivalent to [^\f\t\v]
\f matches a page feed character equivalent to \x0c or \CL
Match a newline character; equivalent to \x0a or \CJ
Matching a return character is equivalent to \x0d or \cm
\ t matches a tab character, equivalent to \x09\ or \CL
\v matches a vertical tab; equivalent to \x0b or \ck
\onn match a octal number
\XNN matches a hexadecimal digit
\CC matches a control character
mode modifier (pattern modifiers):
The pattern modifier is especially used in ignoring case and matching multiple lines, and mastering this modifier often solves many of the problems we encounter.
I-can match uppercase and lowercase letters at the same time
M-Treat a string as multiple lines
S-Treats the string as a single line, and the newline character is treated as ordinary characters so that "." Match any character
X-whitespace negligible in pattern
U-Match to the nearest string
E-Use the replaced string as an expression
Format:/apple/i matches "Apple" or "apple" and so on, ignoring case. /I
Pcre Mode Unit:
1 extracting first-bit attributes
/^\d{2} ([\w]) \d{2}\\1\d{4}$ matches strings such as "12-31-2006", "09/27/1996", "86 01 4321". However, these regular expressions do not match the format of "12/34-5678". This is because the result "/" of the Mode "[\w]" has been stored. The match pattern is also the character "/" when the next position "\1" is referenced.
Use a non-storage mode unit when you do not need to store matching results (? :)”
For example/(?: ABC) (DEF) \\1g/will match "AEEg". In some regular expressions, it is necessary to use a non-storage-mode unit. Otherwise, the order of subsequent references needs to be changed. The previous example can also be written AS/(ABC) (CEF) \2g/.