Use of regular expressions in PHP programs

Source: Internet
Author: User
Overview:

A regular expression is a matching pattern and is also a string (just some syntax rules, special characters ) in this string.

The regular expression of this string, must be used in the corresponding function, such as split function, replace function, find function, matching function.

Use the Pcre function to represent a regular expression (compatible with Perl)

Regular Expression composition: delimiter, Atom, metacharacters, pattern modifier

1, delimiter: In the string double quotation marks with the//number or other ({}, etc.) to do the delimiter, such as "/efgrehrt/"

2, Atom: The character that needs to be matched is an atom (the smallest matching unit, placed in the delimiter), and if the atom is a special character , such as/, then it needs to be escaped with \ to become \/. There is at least one atom in the regular expression .

(1) Atoms include print characters (A-Z-A-Z 0_9!@ #等) and nonprinting characters (carriage return \n,\.), escape characters \ can turn meaningful characters into meaningless atomic characters.

3, metacharacters: metacharacters cannot be used alone, it is used to extend atomic functions and to define functions (written in delimiters). For example, {5} in "/7{5}/" is a meta-character, which indicates that atomic 7 must appear 5 times

4, pattern modifier: fix, correct the pattern (regular) (the entire expression is corrected, written on the outside of the delimiter, to the right). For example "/a{5}/i", plus I is not case sensitive

Regular Expressions need to be placed in functions to be used: for example $str = "AA3AA4AA5AA6AA8AAA9AA" $reg = "/\d/" Preg_replace ($reg, "C", $str), replacing numbers in $str with C characters

The above describes the use of regular expressions in PHP programs, including special characters, regular expression aspects of the content, I hope that 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.