A recent study of regular expressions. See a lot of friends need an IP address regular expression, on the Internet to find a moment, the beginning to find a Moonlight blog site template on the search for a long time did not find a complete resolution of the regular expression of IP address does not matter, I casually verified the above a regular expression results do not match, so I wrote a, Share with you that there may be a lot of mistakes I hope you will be more guidance, forgive me.
In many cases, some compatibility rules need to be added because the new rules are constantly appearing. The regular expression of learning is the idea of learning, copycat is not good.
1. $reg = '/[\x-\x]+/u ';//verify is a Chinese character
2. $reg = '/\n\s*\r/';//verify is blank line
There's going to be a lot of people out there who don't know what a blank line means. I will operate here, as follows
Copy Code code as follows:
<?php
$str = "";
$reg = '/\n\s*\r/';
if (Preg_match ($reg, $str)) {
echo "Validation through blank lines";
}else{
echo "does not validate through blank lines";
}
?>
This will solve some code redundancy
3. $reg = '/<[\/]?\w+>/is '//verify HTML start and end tags
4. $reg = '/^[\s*]| [\s*]$/';//a regular expression that matches the first and last whitespace characters (starting with a blank character or ending with him)
5. $reg = '/^0\d{2,3}-[1-9]\d{6,7}$/';//Regular expression that matches the home phone address:
6. $reg = '/^[1-9][0-9]{4,}$/';//Match Tencent QQ number
7. $reg = '/^\d$/';//Postal Code of mainland China
8. $reg = '/^[1-9]\d$/';//15 identity cards in mainland China
$reg = '/^[1-9]\d (\d|x|y) $/'; the 18-digit ID in mainland China includes the following letters.
9. $reg = '/^ (25[0-5]|2[0-4][0-9]|[ 0-1][0-9]| [1-9] [0-9]| [1-9]) \. (25[0-5]|2[0-4][0-9]| [0-1] [0-9]| [1-9] [0-9]| [1-9]|0) \. (25[0-5]|2[0-4] [0-9]| [0-1] [0-9]| [1-9] [0-9]| [1-9]|0) \. (25[0-5]|2[0-4][0-9]| [0-1] [0-9]| [1-9] [0-9]| [0-9]) $/'//Verify IP
Here I also explain the following:
IP is divided into 5 categories:
Class A 1.0.0.0~126.0.0.0
Class B 128.0.0.0~191.255.255.255
Class C 192.0.0.0~223.255.255.255
Class D 224.0.0.0~239.255.255.255
Class E 240.0.0.0~255.255.255.255
Explain first (25[0-5]|2[0-4][0-9]|[ 0-1][0-9]| [1-9] [0-9]| [1-9]) What is the meaning. We first divided the IP into 4 sections of the easy to understand some. The first section, 250~255 or 200~249 or 100~199 or 10~99 or 1~9 is not very clear, the first section can not be 0. And so on the back.
$reg = '/^[1-9][0-9]*$/';//matching positive integer
$reg = '/^-[1-9][0-9]*$/';//Match negative integer
$reg = '/^-? (0| ([1-9]\d*)] $/';//Match integer (here to note the use of parentheses)
$reg = '/^ ([1-9]\d*\.\d*) | (0\.\d*) $/';//matching positive floating-point number
$reg = '/^ (0| ( [1-9]\d*[0|2|4|6|8]) $/'//number of spouses
$reg = '/^[a-za-z0-9]\w{5,17}@[a-za-z0-9] ([a-z]|[ a-z]| [0-9]|_) + (\.[ a-za-z]{2,6}) {1,2}$/';//Regular expression matching email address:
$reg = '/^[a-za-z]\w{5,19}$/';//match user name must begin with a letter, a letter, a number, an underscore, and a length of 6~20
This article is purely a personal point of view is absolutely not targeted, if there is a mistake please correct me
More about common regular expressions can be seen here.