PHPPerl regular expression-php Tutorial

Source: Internet
Author: User
Tags perl regular expression
PHPPerl-style regular expressions 1. regular expressions, as a matching template, are composed of atoms (common characters, such as a-z) and have special functions (metacharacters, for example, *, +, and? And the pattern modifier.
2. when using a pattern in Perl-compatible regular expression functions, you must add a separator to the pattern, that is, the pattern contains two backslash/s.

1. delimiters
When Perl-compatible regular expressions are used, the pattern expressions must be placed between delimiters. As the delimiter, it is not limited to "/". any character except numbers, characters, and backslash \ can be used as the delimiter. However, it is usually used to saying that a pattern expression is contained between two diagonal lines.

II. atom
An atom is the most basic unit of a regular expression, and each pattern must contain at least one atom.
1. normal characters as atoms
A ~ Z, ~ Z, 0 ~ 9.

2. some special characters and metacharacters are used as atoms
Any symbol can be used as an atom, but if the symbol represents some special meanings in a regular expression. Escape characters must be used to cancel special meanings and convert them into common atoms.

3. some non-printable characters are used as atoms

The so-called non-printable characters are some format control symbols in strings. For example, space, carriage return, and tab.


4. use "general character type" as the atom
Whether printed or non-printed characters are used as atoms, an atom can only match one character. In some cases, we need an atom to match a class of characters. Such as matching numbers and letters.

5. use a custom atomic table ([]) as an atom

You can use custom "class atoms" to define atoms that are equal to each other.
For example, "/[jhp] sp/" can match three types


3. metacharacters
The Perl regular expression can also be used to search for and match various metacharacters. Metacharacters are special characters used to construct regular expressions. Such .*? +. In a regular expression, metacharacters cannot appear separately. they are used to modify atoms. Just like the adjectives in Chinese, we must have a descriptive subject to wake up. For example, I am very handsome. Shuai is an adjective, describing me. Metacharacters can be understood and imagined in this regard.
The method for constructing a regular expression is the same as that for writing an article. it uses a variety of adjectives to clearly and intuitively express a person or thing. The regular expression component can be a single character, character set combination, character range, choice between characters, or any combination of all these components. These characters are divided into several small classes and described separately.


1. qualifier. Number of times.
2. boundary restrictions

3. ORIGIN. apart from characters, the origin in the mode can match any character in the target, including non-printable characters but not line breaks. If the pattern modifier s is set, the linefeed is also matched.

4. any group of the pattern selection operator (|) matching options.

5. Mode unit. The metacharacter () is used to separate multiple atoms into a large atom and is used as a unit.

6. backward reference.

7. pattern matching priority: from left to right.

4. pattern modifier.

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.