How to use regular expressions to match a range of numbers
Today, I will talk to you about how to use regular expressions to match the number range. Many people may not know much about it. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope everyone can gain something according to this article.
Recently, there is a demand for batch removal of products according to product numbers. Products with removal dates between 16 and 31 are required.
For example, the matching expression for B201607280023 is as follows:
^201607(1[6-9]|2[0-9]|3[0-1]).+
The logic is very simple, if it must be 1 or 2 or 3 beginning, if it is 1 beginning then the following range is 6-9, if it is 2 beginning then 0-9, if it is 3 beginning then 0-1, because July to 31
Similarly, if you want to match the data of the first half month, it is:
^B201607(0[0-9]|1[1-5]).+
php regular expressions limit the number of digits
How to write a regular expression, only 4 numbers can match, such as 1234 can match, 12345 or 12 numbers can not match if the number is not 4?
\d{4} I tried. No, 12345 matches more than 5 digits. No
^[0-9]{4}$Yes, Change Question: Why\d{4} No?
\d{4} This means that at least 4 consecutive numbers can be matched, that is, 123 must not match, but like 1234,12345,1234abc, as long as there are 4 consecutive numbers, they can match. If you want to match only four digits, you have to add the start and end characters ^\d{4}$
Regular expressions match positive integers between 1 and 255
$p = '/^(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[1-9])$/';
for($i=1; $i