Java Regex字符串:检查ip:port字符串是否包含有效的IPv4或DNS地址 [英] Java Regex String: Check if ip:port string contains a valid IPv4 or DNS address

查看:84
本文介绍了Java Regex字符串:检查ip:port字符串是否包含有效的IPv4或DNS地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在尝试在不使用Java外部库的情况下检查String是否是有效的IP地址(它不一定是另一端具有正常工作主机的IP;它可以是未使用的IP.也).我无法检查它是否是IP地址,因为我也希望获得DNS支持.由于以下选项有效:

Basically, I'm trying to check if a String is a valid IP address without using external libraries in Java (it doesn't have to be an IP with a working host at the other end; it can be an unused one too). I can't check if it's an IP address because I would like DNS support as well. Since the following options are valid:

  • 1.2.3.4:55555
  • 本地主机:1
  • google.com:12345
  • 1.2.abc:54321(这有效吗?)

但以下无效:

  • 1.2.3.4(无端口)
  • 1.2:34(不是IP地址)
  • 本地主机:12345(空格)
  • 8.8.8.8:abc(端口插槽中没有数字)
  • stackoverflow.com:234.5(基本上,数字只能跟在:
  • localhost:65536(端口号超出范围)

此刻(我将继续更新),我进行了以下检查:

At this moment (I will continue to update this), i do the following checks:

host != null;
!host.contains(" ");
host.contains(":");

我认为可以进行其他检查,但我不知道该怎么做:

Other checks I think can be done but I don't know how to do:

*仅包含1个冒号*如果仅包含数字和句点,则必须包含3个和仅3个句点,4个数字,并且每个数字必须介于0到255之间*冒号后面的数字是一个数字,介于0到65535之间

*Contains 1 and only 1 colon *If it only contains numbers and periods, it must contain 3 and only 3 periods, 4 numbers, and each number has to be between 0 and 255 *Number following colon is a number and is between 0 and 65535

谢谢!

推荐答案

我会使用这样的正则表达式:

I would use a regex like this :

import java.util.regex.Pattern;

public class IpCheck {

    public static void main(String[] args) {

        Pattern p = Pattern.compile("^"
                + "(((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}" // Domain name
                + "|"
                + "localhost" // localhost
                + "|"
                + "(([0-9]{1,3}\\.){3})[0-9]{1,3})" // Ip
                + ":"
                + "[0-9]{1,5}$"); // Port

        System.out.println(p.matcher("1.2.3.4:55555").matches());
        System.out.println(p.matcher("localhost:1").matches());
        System.out.println(p.matcher("google.com:12345").matches());
        System.out.println(p.matcher("1.2.abc:54321").matches());

        System.out.println(p.matcher("1.2.3.4").matches());
        System.out.println(p.matcher("1.2:34").matches());
        System.out.println(p.matcher("local host:12345").matches());
        System.out.println(p.matcher("stackoverflow.com:234.5").matches());
        System.out.println(p.matcher("localhost:65536").matches());

    }

}

打印:

true
true
true
true
false
false
false
false
true

除了端口范围外,它都可以工作,为此,我建议您提取端口号(如果有),并在正则表达式之外.我的观点是Regex不能测试整数范围(仍然可能)

It works except for the port range, for this I would suggest that you extract the port number if any, and outside the regex. My opinion is that Regex is not made to test integers range (still possible)

这篇关于Java Regex字符串:检查ip:port字符串是否包含有效的IPv4或DNS地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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