Home >Backend Development >PHP Tutorial >Tutorial on basic syntax of regular expressions
The following editor will bring you a detailed application explanation of the basic grammar of regular expressions (a must-read article). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
1. Basic syntax of regular expressions
Two special symbols '^' and '$ '. Their function is to indicate the beginning and end of a string respectively. Examples are as follows:
"^The": indicates all strings starting with "The" ("There", "The cat", etc.);
"of despair$": indicates so A string ending with "of despair";
"^abc$": Indicates a string that starts and ends with "abc" - haha, there is only "abc" itself;
"notice": Represents any string containing "notice".
Like the last example, if you don't use two special characters, you are indicating that the string you want to find is within any part of the string being searched for - you are not positioning it at the top.
Other symbols include '*', '+' and '?', which represent the number of times a character or a sequence of characters appears repeatedly. They mean "none or more", "once or more" and "none or once" respectively. Here are a few examples:
"ab*": Indicates that a string has an a followed by zero or several b. ("a", "ab", "abbb",...);
"ab+": Indicates that a string has an a followed by at least one b or more;
" ab?": Indicates that a string has an a followed by zero or one b;
"a?b+$": Indicates that there is zero or one a followed by one or several b at the end of the string .
You can also use a range, enclosed in curly brackets, to indicate the range of repetitions.
"ab{2}": Indicates that a string has an a followed by 2 b ("abb");
"ab{2,}": Indicates that a string has a a followed by at least 2 b;
"ab{3,5}": Indicates that a string has an a followed by 3 to 5 b.
Please note that you must specify the lower limit of the range (eg: "{0,2}" instead of "{,2}"). Also, you may have noticed that '*', '+' and '?' are equivalent to "{0,}", "{1,}" and "{0,1}".
There is also a '¦', which means "or" operation:
"hi¦hello": indicates that there is "hi" or "hello" in a string;
"(b¦cd)ef": represents "bef" or "cdef";
"(a¦b)*c": represents a string of mixed "a" and "b" followed by A "c";
'.' can replace any character:
"a.[0-9]": Indicates that a string has an "a" followed by an arbitrary character and A number;
"^.{3}$": indicates a string of any three characters (length is 3 characters);
The square brackets indicate that certain characters are allowed in a Appears at a specific position in the string:
"[ab]": indicates that a string has an "a" or "b" (equivalent to "a¦b");
"[a-d]": Indicates that a string contains one of lowercase 'a' to 'd' (equivalent to "a¦b¦c¦d" or "[abcd]");
"^[a-zA-Z]": represents a string starting with a letter;
"[0-9]%": represents a number preceded by a percent sign;
",[a-zA-Z0-9]$": Indicates that a string ends with a comma followed by a letter or number.
You can also use '^' in square brackets to indicate unwanted characters. '^' should be the first one in the square brackets. (For example: "%[^a-zA-Z]%" means that letters should not appear between two percent signs).
In order to express verbatim, you must add the transfer character '\' before the characters "^.$()¦*+?{\".
Please note that within square brackets, no escape characters are required.
2. Regular expression verification controls the input character type of the text box
1. Only numbers and English can be entered:
2. Only numbers can be entered:
3. Only full-width characters can be entered:
4. Only Chinese characters can be entered:
3 , Popular explanation of application examples of regular expressions
//Verify whether it is composed entirely of numbers
/^[0-9]{1,20}$/
^ means that the leading character should match the rule immediately following ^
$ means that the leading character should match the rule immediately preceding $
[ ] The content is Optional character set
[0-9] means the required character range is between 0-9
{1,20} means the legal length of the numeric string is 1 to 20, which is [ The number of occurrences of characters in 0-9] ranges from 1 to 20 times.
/^ and $/ should be used in pairs to indicate that the entire string is required to completely match the defined rule, rather than only matching a substring in the string.
//Verify login name: You can only enter 5-20 strings starting with letters and including numbers, "_", and "."
/^[a-zA -Z]{1}([a-zA-Z0-9]|[._]){4,19}$/
^[a-zA-Z]{1} means the first one Character requirements are letters.
([a-zA-Z0-9]|[._]){4,19} means starting from the second digit (because it immediately follows the previous expression) with a length of 4 A string of up to 9 characters, which must be composed of uppercase and lowercase letters, numbers, or the special character set [._].
//Verify user name: Only 1-30 strings starting with letters can be entered
/^[a-zA-Z]{1,30}$/
//Verify password: Only 6-20 letters, numbers, and underscores can be entered
/^(\w){6,20}$/
\w: Used to match letters, numbers or underline characters
//Verify ordinary telephone and fax numbers: it can start with "+" or numbers, and can contain "-" and " "
/^[ +]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/
\d : Used to match numbers from 0 to 9;
The "?" metacharacter specifies that its leading object must appear zero or once in the target object
Strings that can be matched such as: +123 -999 999; +123-999 999; 123 999 999; +123 999999, etc.
//Verify URL
/^http[s]{0,1}:\ /\/.+$/ or /^http[s]{0,1}:\/\/.{1,n}$/ (indicates that the length of the url string is length(“https://”) + n )
\ /: Represents the character "/".
. Represents the set of all characters
+ is equivalent to {1,}, which is from 1 to positive infinity.
4. Regular expression application (common parts)
##"^\d+$" //Non-negative integer (positive Integer + 0) "^[0-9]*[1-9][0-9]*$" // Positive integer "^((-\d+)| (0+))$" //Non-positive integer (negative integer + 0) "^-[0-9]*[1-9][0-9]*$" //Negative integer "^-?\d+$" //Integer "^\d+(\.\d+)?$" //Non-negative floating point number (positive floating point number + 0)"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0 -9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //Positive floating point number"^( (-\d+(\.\d+)?)|(0+(\.0+)?))$" //Non-positive floating point number (negative floating point number + 0) "^(- (([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[ 0-9]+)|([0-9]*[1-9][0-9]*)))$" //Negative floating point number "^(-?\d+)( \.\d+)?$" //Floating point number"^[A-Za-z]+$" //A string consisting of 26 English letters"^[ A-Z]+$" //A string consisting of 26 uppercase English letters"^[a-z]+$" //A string consisting of 26 lowercase English letters"^[A-Za-z0-9]+$" // A string consisting of numbers and 26 English letters "^\w+$" // A string consisting of numbers, 26 English letters or String composed of underscores"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$" //email address"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\ ?\S*)?$" //url/^(d{2}|d{4})-((0([1-9]{1}))|(1[1 |2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // Year-month-day/ ^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0| 1]))/(d{2}|d{4})$/ // Month/Day/Year"^([w-.]+)@(([[0-9] {1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z] {2,4}|[0-9]{1,3})(]?)$" //Emil"(d+-)?(d{4}-?d{7}| d{3}-?d{8}|^d{7,8})(-d+)?" //Phone number"^(d{1,2}|1dd|2[0 -4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2 [0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$" //IP address^([0-9A-F]{2})(-[0-9A-F]{2}){5}$ //Regular expression of MAC address^[-+ ]?\d+(\.\d+)?$ //Value type regular expressionThe above is the detailed content of Tutorial on basic syntax of regular expressions. For more information, please follow other related articles on the PHP Chinese website!