For example, if you want to match this
22.22.22.0/16
33.33.33.1/24
How to write such a regular change? Please give me some advice
This seems to work
/((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)\/[0-2][0-4]/
PHP中文网2017-07-05 10:40:27
Given in "Must Know Regular Expressions". It’s easy to make mistakes when you write it yourself
过去多啦不再A梦2017-07-05 10:40:27
^((0|1[0-9]{0,2}|2[0-9]{0,1}|20-4|25[0-5]|3-9{0,1}).){3}(0|1[0-9]{0,2}|2[0-9]{0,1}|20-4|25[0-5]|3-9{0,1})(?(/)/([0-9]|1-2|3[0-2])|)$
为情所困2017-07-05 10:40:27
You can directly find a regular match for IP on the Internet, but the one on the Internet may not be correct, and it is not easy to verify, so we will write one ourselves.
There is a tool for generating regular expressions from a numerical range. [Tools can be found online]
ip rules0-255.0-255.0-255.0-255/0-32
([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25 [0-5])
([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25 [0-5]).
(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]| 25[0-5]).){3}
(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]| 25[0-5]).){3}([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0 -4][0-9]|25[0-5])
([0-9]|[12][0-9]|3[0-2])
(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]| 25[0-5]).){3}([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0 -4][0-9]|25[0-5])/([0-9]|[12][0-9]|3[0-2])
^(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9] |25[0-5]).){3}([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[ 0-4][0-9]|25[0-5])/([0-9]|[12][0-9]|3[0-2])$
or(?^ |s)(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9] |25[0-5]).){3}([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[ 0-4][0-9]|25[0-5])/([0-9]|[12][0-9]|3[0-2])(?:s|$)
The dividing symbols are replaced according to the actual situation
If no separator is added, 33.33.33.1/24
will be matched into 33.33.33.1/2
or 99933.33.33.1/24
will also be matched