如何匹配IPv4地址 [英] How to match IPv4 addresses

查看:104
本文介绍了如何匹配IPv4地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的正则表达式匹配以下内容:

I am trying to have my regex match the following:

169.254.0.0-169.254.254.255

任何人都可以帮助我如何实现这一目标.

Could anyone please help how can I achieve this.

到目前为止,我有这个:

so far I have this:

169\.254\.([1-9]{1,2}|[1-9]{1,2}[1-4])

但它也会获得169.254.255.1,这不应该是比赛之一.

but it would also pick up 169.254.255.1 which should not be one of the matches.

请帮助!

谢谢

推荐答案

这是我用于常规IP验证的正则表达式:

This is the regex I use for general IP validation:

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

故障:

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

这并没有使我的IP地址验证失败.

This hasn't failed my yet for IP address validation.

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

For your specific case, this should do it:

(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])))

这很长,因为我不知道如何在不使169.254.255.1失败的情况下进行169.254.(0-254).255的检查

This is very long because I couldn't figure out how to get 169.254.(0-254).255 to check without getting 169.254.255.1 to fail

由于评论而固定

这篇关于如何匹配IPv4地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆