Home > Article > Backend Development > Detailed basic example analysis of regular expressions
Regular expressions are often seen in our verification! Let me share with you how to understand regular expressions more simply.
First of all, let’s talk about the naming of regular names: I think everyone will be familiar with this word
! "Regular Expression". The "Regular" of Regular Expression is generally translated as "regular", "regular", and "conventional". "Regular" here means "rules" and "laws", and Regular Expression means "an expression that describes a certain rule", which is what we call regular expressions. In fact, regular expressions are used to regulate certain behaviors! In other words, it is a constraint, just like we must obey traffic rules.
I personally feel that it is not difficult to understand and understand regular rules! Just add some of the things that must be remembered and use them flexibly, and that's it!
OK! Next, I will delve into the main body of regularity.
Let’s first look at the key things of regularity. If we understand these things! Generally there will be no problem in the project!
The first "\"
This is commonly known as an escape character, which marks a character as a special character or literal character. For example: "n" matches "n". If it is "\n", it is a newline character.
Someone should ask, what if I just want to write the slash "\"? This is also very simple! Just write "\\" like this! Why write two "\\"! Just to differentiate.
The second "^"
This is commonly known as the starting character, which means you are ready to write regular expressions! If the Multiline property of the RegExp object is set, ^ also matches the position after "\n" or "\r".
The third "$"
This is commonly known as the ending character, which can also be said to be the ending (a very unprofessional explanation)! If the Multiline property of the RegExp object is set, $ also matches the position before "\n" or "\r"
The fourth "*"
This matches the previous one Zero or more subexpressions. For example: zo* can match "z" and "zo" or "zoo". This "*" is equivalent to {0,}
The fifth " "
matches the previous subexpression one or more times. For example: "zo" can match "zo" and "zoo" or "zooo". This "*" and " " are almost the same. One starts with zero times and the other starts with once. This " " is equivalent to {1,}.
The sixth "?"
This matches the previous subexpression zero or once. For example: "do(es)?" can match "do" or "does". This question mark means either matching zero times or matching once!
The seventh "{}"
How many times does this symbol match?
1,{n} matches a certain number of n times, n is a non-negative integer , such as: "o{2}" means matching two "oo", such as: good, food, etc.! But it cannot match body, because there is only one o!
2,{n,} matches at least n times, n is a non-negative integer, such as: "o{2,}" This means matching more than two "oo", such as: good, goood, gooood, etc. . "o{1,}" is equivalent to "o". "o{0,}" is equivalent to "o*".
3,{n,m} is a minimum match of n times and a maximum of m matches. n and m are both non-negative integers, where n<=m. For example: "o{1,3}" matches body, food, food. Doesn't match fooood though. "o{0,1}" is equivalent to "o?". When writing here, please note that there cannot be a space between the comma and the two numbers.
The eighth "?" special usage
When this character immediately follows any other limiter (*, ,?, {n}, {n, }, {n,m}), the matching pattern is non-greedy. The so-called non-greedy means that the least is better. The non-greedy mode matches as few of the searched strings as possible, while the default greedy mode matches as many of the searched strings as possible. For example, for the string "oooo", "o ?" will match a single "o", while "o" will match all "o"s.
The ninth "."
matches any single character except the newline character "\n". If you want to match any character within the newline character "\n", use the "(.|\n)" pattern.
The tenth "pattern"
This "pattern" is not easy to understand. I was confused at first glance! However, my understanding of this is as follows, I hope it is useful to everyone:
1. ?:pattern matches pattern but does not obtain the matching result, for example: k(?:1|2|3) k matches any one of 123, example: k1|k2
2. ?=pattern Positive positive preview For example: K(?=1|2|3) Select K when K matches any one of 123 Example: k in k1 or k# in k2 ##3. ?!pattern forward negative preview. For example: k(?!1|2|3) Select K when K does not match any one of 123. Example: does not match k in k1, but it can be k4, k5
4. ?<=pattern Reverse positive pre-query example: (?<=1|2|3)k Select K when K matches any one of 123 Example: k in 1k or k in 2k K
5. ?
The eleventh "|"
This symbol means or, for example: "f|good" can match "f" or "good", what if this is the case"(f| g)ood" matches "food" or "good".
The twelfth "[]"
This symbol means the sum of character sets. It looks similar to "{}", but its meaning is much different.
The thirteenth "()"
This symbolic array or set
1, [xyz] matches any character contained. That is to say, choose one of the three. Example: "[abc]" can match the "a" in "company" but cannot match "beautiful" because two letters in it are used.
2. [^xyz] This is a set of negative characters, which can also be said to be "non". Example: "[^abc]" can match "drop", etc.! As long as there are no "abc" letters in the word, it's fine.
3. [a-z] character range. Matches any character within the specified range. For example, "[a-z]" matches any lowercase alphabetic character in the range "a" through "z". It can also be written as "[0-9]", which matches numbers from 0 to 9 directly.
4. [^a-z] I think it goes without saying that everyone should know what it means, right! It means what you think: any character that is not in the range of "a" to "z", at first I When I saw this, I thought it was a letter that was not between a and z! I said that if there is no letter between a to z, then there is only "ü" in Chinese! This seems to read "Yu"! hehe! Everyone can see it clearly! It's a character, not a letter.
Let's take a look at the special meanings of "\" and letters.
"\b" is the boundary that matches a word, which means the position between the word and the space. For example, "er\b" matches the "er" in "never" but not the "er" in "verb". I think this is easier to remember. You can remember it like this: the edge of the boundary starts with b!
"\B" is the opposite of "\b" and matches non-word boundaries. "er\B" can match the "er" in "verb", but not the "er" in "never".
"\d" is used more often! I suggest you keep this in mind. This matches numeric characters, which is equivalent to [0-9].
"\D" is also easy to understand. It also means the opposite meaning that it is not a number, equivalent to [^0-9].
"\f" matches a form feed character. No need to explain too much on this! The following four are too many to explain too much. Just remember it! Just use it in your project!
"\n" matches a newline character.
"\r" matches a carriage return character.
"\t" matches a tab character.
"\v" matches a vertical tab character.
"\s" matches any null character, matches any whitespace character, including spaces, tabs, form feeds, etc. Equivalent to [ \f\n\r\t\v]. That is, this one includes all the five above!
"\S" is a non-whitespace character equivalent to [^ \f\n\r\t\v].
Speaking of this, everyone may feel that the regular rules are actually these characters! And some of them can be deduced by our logical thinking, and some of them are repetitive, as long as everyone can use them flexibly.
"\w" matches any word character including underscores. Equivalent to "[A-Za-z0-9_]". This is used quite a lot in practice and I suggest you keep this in mind.
"\W" matches non-word numeric characters. Equivalent to "[^A-Za-z0-9_]".
OK! Basically, that’s all you need to remember! Some regex masters may say to these, "You are not complete at all?" Let me explain it in advance. What I have written is only some basic ones that are common in projects and more practical. Basically, these can be used in projects. Use it freely.
Next, let’s do some substantive things with everyone and parse some regular expressions with everyone.
For example, this regular rule: ^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0 -9])$
I think regular regex experts will know what it is at a glance. Of course, some people with strong logical thinking will know what this is after just two glances. Yes, it is the law of time.
OK Let's analyze this regular expression starting from this "^", "([0-1]?[0-9]|2[0-3])" is a group, "[0-1 ]?" The function of this question mark is that there can be at most zero or one 0 or 1, "[0-9]" is any number between 0 and 9, "|" means "or", which means it is not "[ 0-1]?[0-9]" is "2[0-3]", "2[0-3]" is the first 2, which means 2, and the last 0 to 3 is any one between 0 and 3. Number, ":" means ":", "([0-5][0-9])" is also a group, "[0-5]" is any number between 0 and 5, "[0- 9]" is any number between 0 and 9, ":" is also the original meaning, "([0-5][0-9])" is also a group, "[0-5]" is between 0 and 5 Any number, "[0-9]" is any number between 0 and 9, and "$" is the terminator.
Let’s analyze a decimal with everyone
For example: ^[1-9] \d*(\.[0-9]{1,2})?|0(\.[0-9]{1,2})?$
"^" is the start character, "[1-9]" where " " means at least one or more between 1 and 9, "\d*" this "\d" is a number, this "*" is There are at least zero numbers or multiple numbers, "(\.[0-9]{1,2})?" In this group, "\." is the original point, "[0-9]{1,2} "There is one or two numbers between 0 and 9. The question mark "?" behind it means there is zero or one number "(\.[0-9]{1,2})". "|" is either "[1-9] \d*(\.[0-9]{1,2})?" or "0(\.[0-9]{1,2})?" ". "0(\.[0-9]{1,2})?" The 0 in this group is the original meaning, and the "\." in this group of "(\.[0-9]{1,2})?" is the original meaning. The original meaning is, "[0-9]{1,2}" There is one or two numbers between 0 and 9, and the question mark "?" behind it means there is zero or one "(\.[0-9 ]{1,2})".
Below I will give you some examples of common regular expressions:
^[1-9]\d*$ //Match positive integers
^-[ 1-9]\d*$ //Match negative integers
^-?[1-9]\d*$ //Match integers
^[1-9]\d*|0$ //Match Non-negative integer (positive integer 0)
^-[1-9]\d*|0$ //Match non-positive integer (negative integer 0)
^[1-9]\d*\.\ d*|0\.\d*[1-9]\d*$ //Match positive floating point numbers
^-([1-9]\d*\.\d*|0\.\d* [1-9]\d*)$ //Match negative floating point numbers
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d *|0?\.0 |0)$ //Match floating point number
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0 ?\.0 |0$ //Match non-negative floating point numbers (positive floating point numbers 0)
^(-([1-9]\d*\.\d*|0\.\d*[1- 9]\d*))|0?\.0 |0$ //Match non-positive floating point numbers (negative floating point numbers 0)
^[a-zA-Z][a-zA-Z0-9_]{ 4,15}$ //Whether the matching account is legal (starting with a letter, 5-16 bytes allowed, alphanumeric underscores allowed)
^\s*|\s*$ //Regular expression matching first and last whitespace characters
\n\s*\r //Regular expression to match blank lines
[^\x00-\xff] //Match double-byte characters (including Chinese characters)
[\u4e00-\ u9fa5] //Regular expression matching Chinese characters
Username
^[a-z0-9_-]{3,16}$
Password
^[a -z0-9_-]{6,18}$
Hex value
^#?([a-f0-9]{6}|[a-f0-9]{3 })$
Email
^([a-z0-9_\.-] )@([\da-z\.-] )\.([a-z\.]{2, 6})$
^[a-z\d] (\.[a-z\d] )*@([\da-z](-[\da-z])?) (\.{1,2} [a-z] ) $
URL
^(https?:\/\/)?([\da-z\.-] )\.([a-z\.]{2,6} )([\/\w \.-]*)*\/?$
IP address
((2[0-4]\d|25[0-5]|[01] ?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)
or
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(? :25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
HTML tag
^< ;([a-z] )([^<] )*(?:>(.*)<\/\1>|\s \/>)$
The above is about regular rules Some basic knowledge of expressions is explained with practical examples. I hope it can help students who are confused about regular expressions. Please point out if there are any mistakes.
For more related questions, please visit the PHP Chinese website: PHP Video Tutorial
The above is the detailed content of Detailed basic example analysis of regular expressions. For more information, please follow other related articles on the PHP Chinese website!