Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从字符串中获取IP地址列表?_Java_Regex_Algorithm - Fatal编程技术网

Java 如何从字符串中获取IP地址列表?

Java 如何从字符串中获取IP地址列表?,java,regex,algorithm,Java,Regex,Algorithm,我有一个包含多个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个

我有一个包含多个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被发现是一个万无一失的解决方案

我想使用正则表达式并获取
列表
ipaddress返回值

public List<String> findIPs(String str) {
 //implement it via regex
}
公共列表findIPs(字符串str){
//通过regex实现它
}
在这里,我想要方法将返回的字符串列表中的3项

我怎样才能通过正则表达式完成它

提前感谢。

这里回答m组(0)是匹配的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)\"";
    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));
    }