Home  >  Article  >  Backend Development  >  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)

慕斯
慕斯Original
2021-06-09 14:42:091484browse

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.;






表单页面

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:

' ;
if ( $result){ 
echo '匹配成功';
}else{
echo '匹配失败';
}
var_dump($match);
?>

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!

Statement:
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