如何匹配IPv4地址

use*_*564 2 regex posix ip-address

我想让我的正则表达式匹配以下内容:

169.254.0.0-169.254.254.255
Run Code Online (Sandbox Code Playgroud)

有谁可以帮助我如何实现这一目标.

到目前为止我有这个:

169\.254\.([1-9]{1,2}|[1-9]{1,2}[1-4])
Run Code Online (Sandbox Code Playgroud)

但它也会拿到169.254.255.1,这不应该是其中一个比赛.

请帮忙!

谢谢

Nic*_*ick 5

这是我用于一般IP验证的正则表达式:

(([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))[.]?){4}
Run Code Online (Sandbox Code Playgroud)

分解:

1.`[0-9](?!\d)`       -> Any Number 0 through 9 (The `(?!\d)` makes sure it only grabs stand alone digits)
2.`|[1-9][0-9](?!\d)` -> Or any number 10-99 (The `(?!\d)` makes sure it only grabs double digit entries)
3.`|1[0-9]{2}`        -> Or any number 100-199
4.`|2[0-4][0-9]`      -> Or any number 200-249
5.`|25[0-5]`          -> Or any number 250-255
6.`[.]?`              -> With or without a `.`
7.`{4}`               -> Lines 1-6 exactly 4 times
Run Code Online (Sandbox Code Playgroud)

这还没有让我的IP地址验证失败.

对于您的具体情况,这应该这样做:

(169\.254\.)((([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}|2[0-4][0-9]|25[0-4])[.])(([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}|2[0-4][0-9]|25[0-5])))
Run Code Online (Sandbox Code Playgroud)

这很长,因为我无法弄清楚如何获得169.254.(0-254).255来检查而不会让169.254.255.1失败

编辑:由于评论而修复