Home > Article > Backend Development > A brief discussion on regular expressions, regular expressions_PHP tutorial
Simply put: Regular expression (Regular Expression) is a language for processing string matching;
Regular expression describes a string matching pattern, which can be used to check whether a string contains a certain substring, and perform "removal" or "replacement" operations on the matched substring.
Regular expressions are very practical in the actual development process and can quickly solve some complex string processing problems. Below I will make some simple classifications of the applications of regular expressions:
For example, if you want to verify whether a string is the correct EMail, Telphone, IP, etc., then it is very convenient to use regular expressions.
For example, if you want to grab a picture from a web page, then you must find the tag. At this time, you can use regular expressions to accurately match it.
For example, if you want to hide the middle four digits of your mobile phone number and change it to this pattern, 123****4567, then it will be very convenient to use regular expressions.
I will briefly introduce regular expressions below:
Note: X represents the character to be found
In addition, there are the following:
Range characters: [a-z], [A-Z], [0-9], [0-9a-z], [0-9a-zA-Z]
Any character: [abcd], [1234]
Except characters: [^a-z], [^0-9], [^abcd]
There are two ways to use regular expressions in Javascript:
The methods provided are:
// If there are subexpressions in the regular expression, when using the exec method
//What is returned is: result[0] = matching result, result[1] = matching result of sub-expression 1...
The methods provided are:
There are two functions using regular expressions under PHP:
The methods provided are:
The methods provided are:
Regular expression is a tool for us to implement a certain function. This tool:
Different combinations of various qualifiers in regular expressions will achieve different functions. Sometimes to implement a complex function requires writing a long regular expression. How to achieve accurate matching will test the ability of a programmer. .
Usually when we search for string content, we can only search for a specific string, but regular expressions can help us perform fuzzy searches, which is faster and more convenient and only requires a regular expression string.
Currently mainstream languages such as JAVA, PHP, Javascript, C#, C++, etc. all support regular expressions.
Learning regular expressions is quick and easy, but how to write efficient and accurate regular expressions in actual development still requires a long period of trial and accumulation.
Regular expressions are often used in js to determine mobile phone numbers, email addresses, etc., to achieve powerful functions through simple methods
Symbol explanation
Character description
\ Mark the next character as A special character, or a literal character, or a backreference, or an octal escape character. For example, 'n' matches the character "n". '\n' matches a newline character. The sequence '\\' matches "\" and "\(" matches "(".
^ matches the beginning of the input string. If the Multiline property of the RegExp object is set, ^ also matches '\n' or ' The position after \r'.
$ matches the end of the input string. If the Multiline property of the RegExp object is set, $ also matches the position before '\n' or '\r'. subexpression zero or more times. For example, zo* matches "z" and "zoo". * is equivalent to {0,}. For example, 'zo+' matches "zo" and "zoo", but not "z". + is equivalent to {1,}? Matches the preceding subexpression zero or once. For example, "do(es". )?" can match "do" in "do" or "does". ? is equivalent to {0,1}.
{n} n is a non-negative integer. Matches a certain n times. For example, ' o{2}' cannot match 'o' in "Bob", but can match two o's in "food".
{n,} n is a non-negative integer, for example, '. o{2,}' cannot match 'o' in "Bob", but it can match all o's in "foooood". 'o{1,}' is equivalent to 'o{0,}' then. Equivalent to 'o*'.
{n,m} m and n are non-negative integers, where n ? When this The matching pattern is non-greedy when the character immediately follows any of the other qualifiers (*, +, ?, {n}, {n,}, {n,m}). The non-greedy pattern searches for as few matches as possible. string, 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. '.
. Matches any single character except "\n". To match any character including '\n', use a pattern like '[.\n]'. y matches x or y. For example, 'z... The rest of the text >>
What is a regular expression? Give an example