The regular expression that we all call now basically refers to a Perl-style regular expression. POSIX-style regular expressions are basically no one to use, so from PHP5.3, it is deprecated, probably to the next version of PHP will be the relevant function deleted.
About regular expressions, because they are too complex to read a regular book in the future, they only introduce functions that are compatible with Perl-style regular expressions.
1. Delimiter
Delimiters denote the beginning and end of a regular expression, usually denoted by a slash (/). In PHP (other languages have not been tested for the time being), it can also be substituted with other non-alphanumeric characters. such as/\d+/and #\d+ #的表示同一个正则表达式 \d+. At the same time, you can also use parentheses, brackets, and curly braces as delimiters, such as [\d+].
2. Functions
Matching function: Preg_match (); and Preg_match_all ();
Replacement function: Preg_replace ();
Split function: Preg_split ();
Filter function: Preg_grep ();
Example code:
Copy the Code code as follows:
$a = <<< TEXT
AAAAAAA 15
BBBBBBB 16
TEXT;
$ret = Preg_match (/(\w+) (\d+)/, $a, $match);
$ret: 1
$match: Array (aaaaaaa 15′, AAAAAAA, 15′)
$ret = Preg_match_all (/(\w+) (\d+)/, $a, $match);
$ret: 2
$match: Array (
Array (aaaaaaa 15′, bbbbbbb 16′),
Array (BBBBBBB, BBBBBBB),
Array (15′, 16′),
// )
$ret = Preg_match_all (/(\w+) (\d+)/, $a, $match, preg_set_order);
$ret: 2
$match: Array (
Array (aaaaaaa 15′, BBBBBBB, 15′),
Array (bbbbbbb 16′, BBBBBBB, 16′),
// )
$b = Preg_replace (/(\w+) (\d+)/, \1, \2′, $a);
$b: AAAAAAA, 15
BBBBBBB, 16′
$c = Preg_split (/\s/, $a);
$c: Array (aaaaaaa, 15′, BBBBBBB, 16′)
$files = Array (aa.txt, Bb.xls, cc.txt);
$txtFiles = Preg_grep (/.*\.txt/, $files);
$txtFiles: Array (aa.txt, cc.txt)
Resources:
PHP Programming, 2003, fourth string, regular expression
The above describes the Photoshop Learning site PHP learning regular expression, including the Photoshop learning site content, I hope the PHP tutorial interested in a friend helpful.