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
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. ?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] )([^(.*)|\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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool