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:
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:
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 |
- Course Recommendations
- Courseware download
-
IntermediateFront-end Vue3 actual combat [handwritten vue project]
2857 people are watching -
ElementaryAPIPOST tutorial [Popularization of technical concepts related to network communication]
1795 people are watching -
IntermediateIssue 22_Comprehensive actual combat
5521 people are watching -
ElementaryIssue 22_PHP Programming
5172 people are watching -
ElementaryIssue 22_Front-end development
8713 people are watching -
IntermediateBig data (MySQL) video tutorial full version
4525 people are watching -
ElementaryGo language tutorial-full of practical information and no nonsense
2794 people are watching -
ElementaryGO Language Core Programming Course
2814 people are watching -
IntermediateJS advanced and BootStrap learning
2563 people are watching -
IntermediateSQL optimization and troubleshooting (MySQL version)
3374 people are watching -
IntermediateRedis+MySQL database interview tutorial
2963 people are watching -
ElementaryDeliver food or learn programming?
5708 people are watching
Students who have watched this course are also learning
- Let's briefly talk about starting a business in PHP
- Quick introduction to web front-end development
- Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
- Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
- Login verification and classic message board
- Computer network knowledge collection
- Quick Start Node.JS Full Version
- The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
- Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
- About us Disclaimer Sitemap
- php.cn:Public welfare online PHP training,Help PHP learners grow quickly!
Atomic | Equivalence |
---|---|
\w | [a-zA-Z0-9_] |
\W | [^a-zA-Z0-9_] |
\d | [0-9] |
\D | [^0-9] |
\s | [ \t\n\f\r] |
[^ \ t\n\f\r] |