您如何在Android上验证IP地址? [英] How do you validate an IP address on Android?
本文介绍了您如何在Android上验证IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Android(作为客户端)和PC(作为服务器)之间的简单套接字通信.我正在让用户将IP地址输入到EditText
字段中,并且我想验证IP地址.您如何在Android上验证IP地址?
I am using simple socket communication between Android (as the client) and PC (as the server). I am having the user input the IP address into an EditText
field and I want to validate the IP address. How do you validate an IP address on Android?
推荐答案
API级别8 +:
您可以使用 Patterns.IP_ADDRESS 全局正则表达式
如果您使用android< 2.2:
You may directly include this regex in your project if you target devices with android < 2.2:
private static final Pattern IP_ADDRESS
= Pattern.compile(
"((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(25[0-5]|2[0-4]"
+ "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]"
+ "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
+ "|[1-9][0-9]|[0-9]))");
Matcher matcher = IP_ADDRESS.matcher("127.0.0.1");
if (matcher.matches()) {
// ip is correct
}
这篇关于您如何在Android上验证IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文