0

I have a big string with multiple IP addresses in it.

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)\"";

I took approach the string into string array using split and later check every word which has 3 . in the word. But dint found to be a fool proof solution.

I want to use a regex and get the List<String> ipAddresses return

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

Here I want 3 item in the list of string which the method will return.

How can I do it via regex?

Thanks in advance.

Nicky
  • 1,025
  • 3
  • 15
  • 29

1 Answers1

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));
    }
qfrank
  • 262
  • 3
  • 14