本文介绍了在Java中验证IPv4地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java验证IPv4地址。它应该使用编写,因此它应该有3个点(),没有字符,点之间的数字和数字应该在有效范围内。应该怎么做?

I want to validate an IPv4 address using Java. It should be written using the dot-decimal notation, so it should have 3 dots ("."), no characters, numbers in between the dots, and numbers should be in a valid range. How should it be done?

推荐答案

使用正则表达式非常简单(但请注意,效率低得多,读取难度大于worpet的答案使用Apache Commons Utility)

Pretty simple with Regular Expression (but note this is much less efficient and much harder to read than worpet's answer that uses an Apache Commons Utility)

private static final Pattern PATTERN = Pattern.compile(
        "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");

public static boolean validate(final String ip) {
    return PATTERN.matcher(ip).matches();
}

基于帖子

这篇关于在Java中验证IPv4地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:00