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:
<?php //定义一个变量叫zz,放正则表达示。为了方便大家记忆,如果你英文比较ok,建议把变量名还是写成英文的$pattern。 $zz = '/a/'; $string = 'ddfdjjvi2jfvkwkfi24'; if(preg_match($zz, $string, $matches)){ echo '匹配到了,结果为:'; var_dump($matches); }else{ echo '没有匹配到'; } ?>
Because I hope to match a, and $string does not exist, so it is unsuccessful.
<?php $zz = '/wq/'; $string = 'ssssswqaaaaaa'; if(preg_match($zz, $string, $matches)){ echo '匹配到了,结果为:'; var_dump($matches); }else{ echo '没有匹配到'; } ?>
There is wq after s in the above string, so the match is successful.
Next let’s try matching a space:
<?php $zz = '/ /'; $string = 'sssssw aaaaa'; if(preg_match($zz, $string, $matches)){ echo '匹配到了,结果为:'; var_dump($matches); }else{ echo '没有匹配到'; } ?>
The execution result is as follows:
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
Atoms | Description |
---|---|
\d | matches a 0-9 |
except 0-9 All characters | |
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 |