PHP Regular Expression delimiter and Atom Introduction _php tutorial

Source: Internet
Author: User
Tags php regular expression
In this section we will introduce the basic syntax of the regular expressions in PHP: delimiters and atoms. The content contains the definition of the delimiter, the definition and composition of the atom, and so on. The composition of the atoms is flexible enough to satisfy our need for processing strings. Before we do this, we need to understand a regular expression handler Preg_match () to test it for the convenience of our tutorial example.

First look at the delimiter of the regular expression, the composition of the regular expression, and the Preg_match () function:

1, the delimiter of the regular expression.

Any character other than letters, numbers, and backslashes can be a bounding symbol, such as | |,//, {} 、!! Wait, but be aware that if there is no special need, we use forward slash//as the bounding symbol of the regular expression.

2, the composition of the regular expression.

Let's take a look at this formula:/Atom and metacharacters/pattern modifier

In other words, the atoms and metacharacters of the regular expression are placed between the delimiters, and the pattern modifier is placed outside the delimiter.

3,preg_match () function

We'll explain in detail later, just to help the test, which returns a Boolean value that indicates whether the match was successful.

To understand the above simple content, let us get to the point.

atoms in regular expressions

What is an atom? An atom is the most basic constituent unit of a regular expression and must contain at least one atom. As long as a regular expression can be used alone in the character, is the atom.

This concept may seem blurry, but let's take a look at how atoms are formed in regular expressions.

Atomic Composition Mode
1, all printing (all strings that can be printed on the screen) and nonprinting characters (invisible, such as spaces, line breaks, etc.)

2, if all meaningful characters, want to use as atomic, all use the "\" escape character to escape. such as: \. \* \+ \? \ (\<\>.

Note: the "\" escape character can turn meaningful characters into meaningless characters, and you can convert meaningless characters to meaningful characters. For example: \d represents any decimal number.

3, in a regular expression you can directly use some system-provided representation of the atom, as shown in the following table:

The atom representing the range Description Custom Atomic notation
\d Represents any decimal number [0-9]
\d Represents any character except a number [^0-9]
\s Represents any white space character, space, \n\r\t\f [\n\r\t\f]
\s Represents any non-whitespace [^\n\r\t\f]
\w Represents any one character a-za-z0-9_ [A-za-z0-9_]
\w Represents any non-word, except for any character other than a-za-z0-9_ [^a-za-z0-9_]

4, Custom atomic table (using square brackets []), can match any one atom in square brackets.

In the table above, we have converted the range atoms provided by the system into a custom equivalent way. Since it is impossible for the system to provide all the atoms I need, it is necessary to customize the atomic table, for example, if we want to match letters or numbers, we need to write the Atom [a-za-z0-9].

Here's what to note:

A, the symbol "-" denotes a range, such as [A-z] for lowercase letters A to Z, but do not write [a-9] this form!

B, the symbol "^" is reversed, it must be placed at the beginning of the square brackets, for example, we want to match the non-number, then the atom is [^0-9].

Let's take a look at the use of the regular expression atom, the code is as follows:

Copy CodeThe code is as follows:
$pattern = '/\d/';//numeric Atomic table, which is the pattern of regular expressions
$string = ' DSADSADSA ';//need a matching string
if (Preg_match ($pattern, $string)) {
echo "Regular expression {$pattern}and string {$string}Match success ";
}else{
echo " the regular expression {$pattern} and string {$string} match failed";
}
?>


Note: The atoms in the custom atomic table have a match on the string, and the match succeeds. The square brackets of the custom atomic table are removed, which means that the entire string is matched. such as '/abc/' means that the string must have ABC in order to be matched, and '/[abc]/' means that the string is matched if it contains any one of a, B, and C characters.

You can modify the schema in the above example (that is, the pattern variable $pattern of the regular expression), and then validate the atom of the regular expression we are talking about in this section.

This section is about the delimiters and atoms of regular expressions, and I believe that you will have used the regular expression atom on the basis of practice. In the next section we will introduce the meta-characters in the regular expressions of PHP, and don't miss them.

http://www.bkjia.com/PHPjc/326083.html www.bkjia.com true http://www.bkjia.com/PHPjc/326083.html techarticle in this section we will introduce the basic syntax of the regular expressions in PHP: delimiters and atoms. The content contains the definition of the delimiter, the definition and composition of the atom, and so on. wherein the composition of atoms 10 ...

  • Related Article

    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.