PHP Regular Expression delimiter and atom introduction

Source: Internet
Author: User
Tags php regular expression

1, the delimiter for 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

That is, 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 more detail later, just to help the test, which returns a Boolean value that indicates whether the match was successful.

Learn the simple things above and 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. The concept of

may seem blurry, but let's look at how atoms are constructed in regular expressions.

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

2, if all meaningful characters want to be used as atoms, All are escaped with the "\" escape character. 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-supplied atomic representation of the scope, as shown in the following table:

represents the range of atoms   Description   Custom atomic notation
 \d   represents any decimal number  [0-9]
 \d & nbsp; represents any character other than a number  [^0-9]
 \s   denotes any whitespace character, space, \n\r\t\f< /td>  [\n\r\t\f]
 \s   denotes any non-whitespace  [^\n\r\t\f ]
 \w   denotes any one word a-za-z0-9_  [a-za-z0-9_]
 \w   denotes 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:
<?php
$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 <strong>{$pattern} </strong> and string <strong>{$string}</strong> match successfully";
}else{
echo "<span style=" color:red; > Regular Expression {$pattern} and string {$string} match failed </span> ";
}
?>



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.

Transferred from: http://www.jb51.net/article/31451.htm

--------------------------------------------------------------------------------------------------------

<?php
Accept the data that the table only son handed over
$STR = $_get[' str '];
$STR = "D";
Declaring a regular expression
$pattern = "/./";

Use a regular function
$result = Preg_match ($pattern, $str, $match);
echo ' match result is: '. $result;
if ($result) {
Echo ' <font color= ' green ' > Match success </font> ';

}else{
Echo ' <font color= ' red ' > match failed </font> ';
}
echo ' Var_dump ($match);
?>

-------------------------------------------------------------------------------------------------

PHP Regular Expression delimiter and atom introduction

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.