What are atoms in regular expressions in PHP? What content does it consist of? (with code)

慕斯
Release: 2023-03-10 12:56:01
Original
1655 people have browsed it

The previous article introduced you to "What is the delimiter of a regular expression? What do we need to pay attention to? (Attached code) 》, this article continues to introduce to you what are atoms in regular expressions in PHP? What content does it consist of? (With code)

What are atoms in regular expressions in PHP? What content does it consist of? (with code)

##Atoms in regular expressions

What is an atom:

The smallest unit that makes up a regular expression is an atom.

What are the contents of atoms:

  • Normal letters are atoms, and all visible characters are atoms.

  • Most invisible characters are also atoms, such as: space, \n, \r, \t

  • \d represents the number 8- Any character between 9. [e-中

  • \D means matching any one character except numbers 0-9

  • \s means matching blank characters , including any character among spaces, \n, \r, \t

  • \S means matching any one character except whitespace characters

  • T \w means matching any character among numbers, uppercase and lowercase letters, and underscores

  • W means matching any character except numbers, uppercase and lowercase letters, and underscores.

  • []Atom list

  • means selecting an atom from the specified characters

  • [ 5-9] represents 5, 6, 7, 8, 9 consecutive characters. It is allowed to be abbreviated to the starting character and ending character

  • [^ ] Exclusion list

  • [^ abc] means that all characters except abc can be used.

First we create a form, then we output some content, enter strings, etc.;


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>表单页面</title>
</head>
<body>
          <form action= "2.php"method= "post">
                <input type="text" name="str" id=""/>
                <input type="submit" value="匹配" >
          </form>
</body>
</html>
Copy after login

Then we enter a ( submit) for matching. After matching, it will be passed to the page we just created. At this time, we are creating a new page. In the new page, do we need to match? What we need to do is those atoms whose content is our regular. Before matching, we first define a string to receive, and then we define a regular expression, because if the regular expression wants to match, it must cooperate with the function, so we have to call the regular matching function (preg_match( )), at the beginning we passed in two parameters, one is the regular expression we defined, and the second is the string we want to match. In fact, we can pass the third parameter which is the matched result (match ), if it matches, it returns 1, if it does not match, it returns 0; so we output the matching result, call the if statement, and judge the output result

The code is as follows:

<?php
//进行匹配
$str = $_POST[&#39;str&#39;];
$pattern = &#39;//&#39; ;
//调用正则匹配函数
$result = preg_match($pattern , $str , $match);
echo &#39; 匹配结果为: &#39;.$result;
echo &#39;<hr/>&#39; ;
if ( $result){ 
echo &#39;<font color="pink" size="5 ">匹配成功</font>&#39;;
}else{
echo &#39;<font color="red">匹配失败</font>&#39;;
}
var_dump($match);
?>
Copy after login

The code result is as follows:

What are atoms in regular expressions in PHP? What content does it consist of? (with code)

When we put $pattern = '//', add a b, ($pattern = '/b/'), see if it is atomic, and then we run It was found that the match was successful;

The code result is as follows:

What are atoms in regular expressions in PHP? What content does it consist of? (with code)

What are atoms in regular expressions in PHP? What content does it consist of? (with code)

It can be seen from the above code running result that b is an atom, so By analogy, we can still get successful matching results when we test B;


Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What are atoms in regular expressions in PHP? What content does it consist of? (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template