Regular Expression
There are two sets of regular expression function libraries in PHP. The two functions are similar, but the execution efficiency is slightly different:
One set is provided by the PCRE (Perl Compatible Regular Expression) Library. Functions with the prefix "prefix;
A set of (default PHP) extensions provided by POSIX (Portable Operating System Interface of Unix ). Use a function prefixed with "ereg;
In PHP, regular expressions have three functions:
Matching is often used to extract information from strings.
Replace the match text with the new text.
Splits a string into a group of smaller information blocks.
A regular expression contains at least one atom.
Atom (common characters, such as English characters)
Metacharacters (special characters)
Pattern correction characters (Regular Expression semantic correction)
Atom)
A single character or number, such as ~ Z, ~ Z, 0 ~ 9.
A pattern unit, such as ABC, can be understood as a large atom composed of multiple atoms.
Atomic table, such as [ABC].
The Mode unit used again, for example, \ 1.
Common escape characters, such as \ d, \ D, \ w
Escape metacharacters, for example :\*,\.
POSIX Regular Expression
POSIX regular expressions are all called Portable Operating System Interface of Unix, meaning UNIX Portable operation System implementation Interface.
The method for constructing a POSIX regular expression is the same as that for creating a mathematical expression, that is, using a variety of metacharacters and operators to combine small expressions to create a larger expression.
Metacharacters (Meta-character)
Metacharacters are special characters used to construct rule expressions. To include metacharacters in a regular expression, you must add "\" before it to escape it.
Metacharacters
* Matches the previous atom 0 times, once, or multiple times
+ Match the previous atom once or multiple times
? Matches the previous atom zero or one time
| Match two or more select columns such as [1-9] | [a-B] | [A-Z] matches with any of them to true
^ Matches the atoms at the beginning of a string, for example, abscd ==^
$ Match the atoms at the end of a string, for example, dasdsv === v $
[] Any atom in the matching square brackets, for example, s === [dsadas]
[^] Match any character except the atom in square brackets, for example, aaaaa === [dddd]
{M} indicates that the first atom exactly appears m times.
{M, n} indicates that the first atom appears at least m times and at least n times (n> m)
{M,} indicates that the first atom appears no less than m times.
() Indicates an atom.
. Match any character except line breaks
^ $ The two original characters are collectively referred to as the delimiter.
Abd = ^ abc $ only matches
Order of pattern matching
Sequence metacharacters
1 () mode Unit
2? * + {} Duplicate match
3 ^ $ Boundary Limit
4 | mode selection
POSIX Regular Expression Functions
Ereg () and eregi ()
Ereg_replace () and eregi_replace ()
Split () and spliti ()
Ereg () and eregi () ereg () string matching functions, eregi () is the version of the ereg () function that ignores the size
Syntax format: if (! Ereg ('^ [^./] [^/] * $', $ userfile) // output die in mismatched format
{
Die ('This is an invalid file name! ');
}
Replace ereg_replace () and eregi_replace (case-insensitive)
String eregi_replace ("Regular Expression", "target replacement character", and "target replacement ")
Syntax format: $ string = "This is a test ";
Echo str_replace ("is", "was", $ string );
Echo ereg_replace ("() is", "\ 1was", $ string); \ 1 is to inherit the first whole
Echo ereg_replace ("() is)", "\ 2was", $ string); \ 2 inherits the second whole
Split () and spliti (case-insensitive) use regular expressions to split strings into Arrays
List: assign some variables to values in the array.
Syntax format: $ date = "04/30/1973 ";
List ($ month, $ day, $ year) = split ('[/. -] ', $ date); // lists the formats of the three variables. // the format in which the variables are split.
Echo "Month: $ month; Day: $ day; Year: $ year <br/> \ n ";
Output result Month: 04; Day: 30; Year: 1973