PHP Perl-style regular expressions

Source: Internet
Author: User
Tags printable characters
1, the regular expression as a matching template, is composed of atoms (ordinary characters, such as A-Z), have special function characters (meta-characters, such as *, + and?). and three parts of the pattern modifier.
2, in the Perl-compatible regular expression function in the use of the pattern must be added to the pattern delimiter, the pattern will contain another two backslash/between.

First, the delimiter
When you use a Perl-compatible regular expression, you put the pattern expression between the delimiters. As delimiters are not limited to using "/", any character except numbers, characters, and backslashes \ Unexpected can be used as a bounding symbol. However, it is usually customary to say that pattern expressions are contained in two slashes/between.

Second, the Atom
Atoms are the most constituent units of regular expressions, and at least one atom is included in each pattern.
1. Ordinary characters as atoms
A~z, a~z, 0 ~ 9, etc.

2, some special characters and metacharacters character as atoms
Any symbol can be used as an atom, but if the symbol represents some special meaning in the regular expression. You must use the escape character to cancel the special meaning and turn it into a normal atom.

3, some non-printable characters as atoms

The so-called nonprinting characters are some form control symbols in the string. such as spaces, carriage returns, and tabs.


4. Use "Universal character type" as Atomic
Either the print character or the nonprinting character as an atom is an atom that can only match one character. And something we need an atom can match a class of characters. such as matching numbers, matching letters.

5. Custom Atomic table ([]) as an atom

Using a custom-defined "class atom", atoms can be used to define the atoms that are at each other's feet.
such as "/[jhp]sp/" can match JSP, HSP, PSP three kinds


Three, meta characters
Perl Regular expressions can also be used to search for matches using a variety of meta-characters. The so-called meta-character is a special-meaning character used to construct regular expressions. Such as. * ? + And so on. In a regular expression, a meta-character cannot appear alone, it is used to modify atoms. Like our Chinese adjectives, we must have a descriptive subject to wake up. For example: I am very handsome. Handsome is an adjective, which describes me. Metacharacters can do this understanding and imagination.
The method of constructing regular expressions is the same as writing an article, which uses many adjectives to express a person or thing clearly and intuitively. A component of a regular expression can be a single character, a character set, a range of characters, a selection between characters, or any combination of these components. These characters are divided into several small classes, respectively.


1, qualifiers. Limit the number of times.
2. Border restrictions

3, the origin, in addition to the characters, the origin in the pattern can match any character in the target, including non-printable characters, but does not match the line break. If the mode modifier s is set, the line break is also matched.

4. The pattern selector (|) matches any of the set of options.

5, Mode unit. Use Metacharacters () to make a large atom of atoms that are used independently as a unit.

6, back to reference.

7. Priority of pattern matching: from left to right.

Four, 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.