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

<?php
//定义一个变量pattern,存放正则表达式
$pattern = '/a/';
//待搜素字段
$string = 'ddfdjjvi2jfvkwkfi24';
//判断,如果匹配到了,输出$matcges的值
if(preg_match($pattern, $string, $matches)){
    echo '匹配到了,结果为:';
    var_dump($matches);
}else{
    echo '没有匹配到';
}
?>
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

<?php
//定义一个变量pattern,存放正则表达式
$pattern = '/f/';
//待搜素字段
$string = 'ddfdjjvi2jfvkwkfi24';
//判断,如果匹配到了,输出$matcges的值
if(preg_match($pattern, $string, $matches)){
    echo '匹配到了,结果为:';
    var_dump($matches);
}else{
    echo '没有匹配到';
}
?>
Result:

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

Next we try to match a space:

Example: as follows

<?php
$zz = '/ /';
$string = 'sssssw aaaaa';
if(preg_match($zz, $string, $matches)){
    echo '匹配到了,结果为:';
    var_dump($matches);
}else{
    echo '没有匹配到';
}
?>
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:

<?php
$zz = '/\d/';

$string = '床9前明月光';

if(preg_match($zz, $string, $matches)){
    echo '匹配到了,结果为:';
    var_dump($matches);
}else{
    echo '没有匹配到';
}

?>

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

Example: The code is as follows

<?php
header("Content-type:text/html;charset=utf-8");
$zz = '/\D/';
$string = '121243中23453453';
if(preg_match($zz, $string, $matches)){
    echo '匹配到了,结果为:';
    var_dump($matches);
}else{
    echo '没有匹配到';
}
?>

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

The example is as follows

<?php
$zz = '/\w/';

$string = '新中_国万岁呀万岁';

if(preg_match($zz, $string, $matches)){
    echo '匹配到了,结果为:';
    var_dump($matches);
}else{
    echo '没有匹配到';
}

?>

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

The examples are as follows

<?php
$zz = '/\W/';

$string = 'afasABCWEQR44231284737';

if(preg_match($zz, $string, $matches)){
    echo '匹配到了,结果为:';
    var_dump($matches);
}else{
    echo '没有匹配到';
}

?>

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

The examples are as follows

<?php
$zz = '/\s/';

$string = "中国万
岁";

if(preg_match($zz, $string, $matches)){
    echo '匹配到了,结果为:';
    var_dump($matches);
}else{
    echo '没有匹配到';
}

?>

8. \S non-empty characters

The example is as follows

<?php
$zz = '/\S/';

$string = "        
          a       ";

if(preg_match($zz, $string, $matches)){
    echo '匹配到了,结果为:';
    var_dump($matches);
}else{
    echo '没有匹配到';
}

?>

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

<?php

$zz = '/[0-5]\w+/';

$string = '6a';

$string1 = '1C';

if(preg_match($zz, $string, $matches)){
    echo '匹配到了,结果为:';
    var_dump($matches);
}else{
    echo '没有匹配到';
}

?>

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:

<?php

$zz = '/[^0-9A-Za-z_]/';

$string = 'aaaaab311dd';

$string1 = '!$@!#%$#^##';

if(preg_match($zz, $string, $matches)){
    echo '匹配到了,结果为:';
    var_dump($matches);
}else{
    echo '没有匹配到';
}

?>

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
||
<?php //定义一个变量pattern,存放正则表达式 $pattern = '/a/'; //待搜素字段 $string = 'ddfdjjvi2jfvkwkfi24'; //判断,如果匹配到了,输出$matcges的值 if(preg_match($pattern, $string, $matches)){ echo '匹配到了,结果为:'; var_dump($matches); }else{ echo '没有匹配到'; } ?>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!