Regular expression of PHP learning in Photoshop learning website

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.