【问题标题】:How to get the list of ipAddresses from the string? [duplicate]如何从字符串中获取 ipAddresses 列表? [复制]
【发布时间】:2018-06-16 15:31:27
【问题描述】:

我有一个包含多个 IP 地址的大字符串。

String str1 = "10.0.0.2 - frank [10/Oct/2000:13:55:36" + " -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326 "
                + "\"104.244.253.29\" \"Mozilla/4.08 " + "[en] (Win98; I ;104.30.244.2)\"";

我使用split 将字符串放入字符串数组,然后检查单词中包含3 个. 的每个单词。但 dint 被发现是一个万无一失的解决方案。

我想使用正则表达式并获得List<String> ipAddresses 返回

public List<String> findIPs(String str) {
 //implement it via regex
}

这里我想要方法将返回的字符串列表中的 3 项。

我怎样才能通过正则表达式做到这一点?

提前致谢。

【问题讨论】:

标签: java regex algorithm


【解决方案1】:
    String str1 = "10.0.0.2 - frank [10/Oct/2000:13:55:36" + " -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326 "
                  + "\"104.244.253.29\" \"Mozilla/4.08 " + "[en] (Win98; I ;104.30.244.2)\"";
    Pattern p = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
    Matcher m = p.matcher(str1);
    while (m.find()){
        System.out.println(m.group(0));
    }

【讨论】:

  • m.group(0) 是匹配的ip
猜你喜欢
  • 1970-01-01
  • 2014-06-14
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 2012-05-03
  • 1970-01-01
  • 2019-02-21
  • 2011-06-09
相关资源
最近更新 更多