19

在应用程序中,我得到包含 IP 地址的字符串,但这些字符串没有精确的格式。我们所知道的是,这些字符串可能包含一个 IP 地址。

这是字符串的外观示例:

  • “XPS端口”
  • “IP_10.29.167.187”
  • “10.29.166.193”

我想获得一个Java代码,如果有一个字符串,则提取字符串的IP地址,或者如果字符串不包含IP地址,则返回“”。

我试过这段代码,但它不起作用:

String IPADDRESS_PATTERN = 
        "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);
Matcher matcher = pattern.matcher(ipString);
        if (matcher.find()) {
            return matcher.group();
        }
        else{
            return "0.0.0.0";
        }

我很确定使用 RegExp 是实现这一目标的最佳方法,但我对这些不太擅长,所以有人可以帮我找到好的 RegExp 吗?

提前致谢。

4

4 回答 4

43

理查德的链接帮助我找到了答案。这是工作代码:

String IPADDRESS_PATTERN = 
        "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";

Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);
Matcher matcher = pattern.matcher(ipString);
if (matcher.find()) {
    return matcher.group();
} else{
    return "0.0.0.0";
}
于 2013-04-08T09:19:10.673 回答
15

IPV4_PATTERN = "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"

于 2013-04-08T08:59:54.923 回答
10

检查此解决方案以验证IPV4 和 IPV6 地址

/**
 * This class provides a variety of basic utility methods that are not
 * dependent on any other classes within the org.jamwiki package structure.
 */
public class Utilities {
  private static Pattern VALID_IPV4_PATTERN = null;
  private static Pattern VALID_IPV6_PATTERN = null;
  private static final String ipv4Pattern = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])";
  private static final String ipv6Pattern = "([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}";

  static {
    try {
      VALID_IPV4_PATTERN = Pattern.compile(ipv4Pattern, Pattern.CASE_INSENSITIVE);
      VALID_IPV6_PATTERN = Pattern.compile(ipv6Pattern, Pattern.CASE_INSENSITIVE);
    } catch (PatternSyntaxException e) {
      //logger.severe("Unable to compile pattern", e);
    }
  }

  /**
   * Determine if the given string is a valid IPv4 or IPv6 address.  This method
   * uses pattern matching to see if the given string could be a valid IP address.
   *
   * @param ipAddress A string that is to be examined to verify whether or not
   *  it could be a valid IP address.
   * @return <code>true</code> if the string is a value that is a valid IP address,
   *  <code>false</code> otherwise.
   */
  public static boolean isIpAddress(String ipAddress) {

    Matcher m1 = Utilities.VALID_IPV4_PATTERN.matcher(ipAddress);
    if (m1.matches()) {
      return true;
    }
    Matcher m2 = Utilities.VALID_IPV6_PATTERN.matcher(ipAddress);
    return m2.matches();
  }
}

source:确定给定的字符串是有效的 IPv4 还是 IPv6 地址。

于 2014-05-26T11:08:05.350 回答
0

IP 地址正则表达式模式:

^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.
([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$

有关示例的参考,请单击此处

于 2017-03-06T17:33:11.877 回答