从使用正则表达式提取字符串中的IP地址(Extract ip addresses from Stri

2019-08-20 06:22发布

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

这里的字符串可以是什么样子的例子:

  • “XPSPort”
  • “IP_10.29.167.187”
  • “10.29.166.193”

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

我想这个代码,但它不工作:

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

我敢肯定,使用正则表达式是实现这一目标的最佳途径,但我不是非常好,这些所谓有人可以帮我找到好的正则表达式?

提前致谢。

Answer 1:

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

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


Answer 2:

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



Answer 3:

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

来源: 确定是否给定的字符串是一个有效的IPv4或IPv6地址。



Answer 4:

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])$

对于示例参考点击这里



文章来源: Extract ip addresses from Strings using regex
标签: java regex ip