Atoms in php regular expression representation

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

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

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

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 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 prove it through experiments:

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

There is wq after s in the above string, so the match is successful.

Next let’s try matching a space:

The execution result is as follows:

QQ截图20161114135142.png

Therefore, $string this There is a space after the w character of the 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.

Specially identified atoms


You need to remember this, 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.

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

\dmatches a value between 0-9

\dmatches a value other than 0-9

The match was successful and matched. Because it is not a character between 0-9.

\w Matches a-zA-Z0-9_

The match is successful and the underscore is matched.

\WMatch a non-a-zA-Z0-9_

Match failed. Because, all the above are a-zA-Z0-9_, and there is nothing that is not a-zA-Z0-9_.

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

The match is successful because there is a carriage return.

\S Non-empty characters

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

[] Specified range of atoms

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

Conclusion:

$string and $string1 both match successfully. Because \w is [a-zA-Z0-9_]

Conclusion:

$string, $string1, $string2 are matched successfully, but $string3 is unsuccessful. Because $string3 exceeds the range of [abc], it starts from d.

[^ character] does not match characters in the specified interval

Conclusion:

1. Matching $string failed , but it succeeds when matching $string1. Because there is a circumflex character inside the square brackets.

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

Summary:

##\D except 0-9 All characters ##\w \W \s \S [ ]
Atoms Description
\d matches a 0-9
a-zA-Z0-9_
except All characters except 0-9A-Za-z_
Matches all whitespace characters\n \t \r Space
Match all non-whitespace characters
Specified range of atoms
Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!
##\S [^ \ t\n\f\r]
Atomic Equivalence
\w [a-zA-Z0-9_]
\W [^a-zA-Z0-9_]
\d [0-9]
\D [^0-9]
\s [ \t\n\f\r]