When I am reading the book called
<the way="" ruby="">,I find there is a very clever regular expression which determines a string whether a string is a valid IPv4 address.The following is the expression:
num = "(\\d|[01]?\ \ d\ \ d|2[0-4]\\d|25[0-5])"
pat = "^#{ num} \.#{ num} \.#{ num} \.#{ num} $"
And the following is for the </the> IPv6 addresses:
num = "[0-9A-Fa-f]{ 0,4} "
pat = "^" + "#{ num} :"*7 + "#{ num} $"
num = "(\\d|[01]?\ \ d\ \ d|2[0-4]\\d|25[0-5])"
pat = "^#{ num} \.#{ num} \.#{ num} \.#{ num} $"
And the following is for the </the> IPv6 addresses:
num = "[0-9A-Fa-f]{ 0,4} "
pat = "^" + "#{ num} :"*7 + "#{ num} $"
本文介绍了一种使用正则表达式验证IPv4及IPv6地址有效性的方法。对于IPv4,通过精确匹配数字范围0到255来确保每个部分的有效性;对于IPv6,则考虑了十六进制字符及前导零的问题。
2640

被折叠的 条评论
为什么被折叠?



