使用正则表达式从字符串中提取IP地址

时间:2013-04-08 08:55:27

标签: java regex ip

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

这是字符串的示例:

  • “XPSPort”
  • “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 个答案:

答案 0 :(得分:35)

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

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";
}

答案 1 :(得分:13)

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}$"

答案 2 :(得分:9)

检查此解决方案以验证 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();
  }
}

来源:Determine if the given string is a valid IPv4 or IPv6 address.

答案 3 :(得分: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])$

有关示例的参考,请点击here