这个正则表达式是否适用于IP验证? [英] Will this regular expression work for IP validation?

查看:92
本文介绍了这个正则表达式是否适用于IP验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个正则表达式来验证字符串是否为IP地址。 (它是一个正确的 IP地址并不重要;这只是一个快速的错误检查,以确保它是那种格式。)这是我到目前为止。它会工作吗?



 ^ \d {1,3} \.\d {1,3} \。\d {1,3} \.\d {1,3} $ 





我能解决的问题看来是它没有验证每个数字是否小于256(例如,你可以输入999.999.999.999),但我不知道如何将正则表达式与数学表达式结合起来,我认为更多的是房间改善情况而不是关键问题。

解决方案





我能看到的问题是它没有验证每个数字是否小于256(例如,你可以输入999.999.999.999),但我不知道如何将正则表达式与数学表达式结合起来,我认为更多一个改进的空间而不是一个关键问题。


看起来应该有效。如果您想要更准确的东西,可以查看这篇文章 [ ^ ]。

I need a regular expression to verify that a string is an IP address. (It's not critical that it be a correct IP address; this is just a quick error check to make sure it's in that format.) This is what I have so far. Will it work all right?

^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$



The problem I can see is that it doesn't verify that each number is less than 256 (eg, you could enter 999.999.999.999), but I don't know how to combine regular expressions with mathematical ones, and I consider that more of a "room for improvement" situation than a critical problem.

解决方案



The problem I can see is that it doesn't verify that each number is less than 256 (eg, you could enter 999.999.999.999), but I don't know how to combine regular expressions with mathematical ones, and I consider that more of a "room for improvement" situation than a critical problem.


That looks like it should work. If you want something more accurate, you can check out this article[^].


这篇关于这个正则表达式是否适用于IP验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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