Basic PHP development tutorial: Atoms in regular expressions

1. Atom

The atom is the smallest unit in the regular expression. To put it bluntly, the atom is the content that needs to be matched. A valid regular expression must contain at least one atom.

All visible and invisible characters are atoms

Note: The spaces, carriage returns, line feeds, 0-9, A-Za-z, Chinese, and punctuation marks we see , special symbols are all atoms.

Before doing the atomic example, let’s first explain a function, preg_match:

Syntax:

int preg_match ( string $regular , string $string[, array &$result] )

Function: Match $string variable based on $regular variable. If it exists, return the number of matches and put the matched results into the $result variable. If no matching result is found, 0 is returned. Note: The above are the main parameters commonly used by preg_match. I did not list several other parameters above. Because the other two parameters are too uncommon.

Let’s verify it through the program:

Example: The code is as follows

Result:

Because what I want is to match a, and $string does not exist, so it is unsuccessful.

Transform this example

Example: The code is as follows

Result:

The above string f exists in, so the match is successful

Next we try to match a space:

Example: as follows

Result:

Because of this, there is a space after the w character of the $string variable. So the match is successful and the string type is output with a length of 1. It's just that our naked eyes are invisible and cannot see this string.

2. Specially marked atoms

19.pngNote: Each one needs to be remembered here Live, it is best to reach the dictation level. When memorizing, remember in pairs. \d matches a 0-9, then \D is all characters except 0-9.

The above has been explained very clearly. We will conduct experiments to learn these step by step.


When you study, please be sure to reach the dictation level for these atoms. Because when we do experiments in the future, you will learn it bit by bit.

Three, \d matches a 0-9

The code is as follows:

Four , \D matches a non-0-9 value

Example: The code is as follows

5. \w matches a-zA- Z0-9_

The example is as follows

6.\Wmatches a non-a-zA-Z0-9_

The examples are as follows

7. \s matches all whitespace characters\n \t \r spaces

The examples are as follows

8. \S non-empty characters

The example is as follows

matches successfully. Although there are spaces, carriage returns and indents on it. However, there is a non-whitespace character a. Therefore, the match is successful.

9. [] Specify the range of atoms

The example is as follows

Try again and change $string to $ string1, see if it matches

Conclusion:
In the above example, 0-5 failed to match $string, but $string1 succeeded. Because, the first value in $string is 6, which is not in the range of [0-5].

10. [^ character] does not match characters in the specified range

The code is as follows:

Conclusion :

Failed to match $string, but succeeded when matching $string1. Because there is a circumflex character inside the square brackets.

^ The function of the circumflex character inside the square brackets is not to match the characters inside the square brackets.

11. Summary

20.png

Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!