10.223.157.186 [15/Jul/2009:14:58:59 -0700] \"GET /favicon.ico HTTP/1.1\" 404 20
ID: 3747337 • Letter: 1
Question
10.223.157.186 [15/Jul/2009:14:58:59 -0700] "GET /favicon.ico HTTP/1.1" 404 209 10.223.157.186 [15/Jul/2009:15:50:35-0700] "GET/HTTP/1.1" 200 9157 10.223.157.186 - - [15/Jul/2009:15:50:35 -0700] "GET /assets/js/lowpro.js HTTP/1.1" 200 10469 10.223.157.186 - [15/Jul/2009: 15:50:35-0700]"GET /assets/css/reset.css HTTP/1.1" 200 1014 10.223.157.186 - - [15/Jul/2009:15:50:35 -0700] "GET /assets/css/960.css HTTP/1.1" 200 6206 10.223.157.186 - - [15/Jul/2009:15:50:35 -0700] "GET /assets/css/the-associates.css HTTP/1.1" 200 15779 10.223.157.186 [15/Jul/2009:15:50:35 -0700] "GET /assets/js/the-associates.js HTTP/1.1" 200 4492 10.223.157.186 [15/Jul/2009:15:50:35-0700] "GET /assets/js/lightbox.js HTTP/1.1" 200 25960 10.223.157.186 - - [15/Jul/2009: 15:50:36 -0700] "GET /assets/img/search-button.gilf HTTP/1.1" 200 168 I'm programing the Pattern regular expression in Java to display the value of a) The column of path of line: example /favicon.ico or /assets/js/lowpro.js b) The first sequence of IP address: example 10.223.157.186Explanation / Answer
Solution:
import java.util.regex.*;
public class Main
{
public static void main(String[] args)
{
String txt="10.223.157.186 - - [15/Jul/2009:14:58:59 -0700] "GET /assets/js/lowpro.js HTTP/1.1" 404 209";
String re1="((?:(?: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]?))(?![\d])"; // IPv4 IP Address 1
String re2=".*?"; // Non-greedy match on filler
String re3="(?:\/[\w\.\-]+)+"; // Uninteresting: unixpath
String re4=".*?"; // Non-greedy match on filler
String re5="((?:\/[\w\.\-]+)+)"; // Unix Path 1
Pattern p = Pattern.compile(re1+re2+re3+re4+re5,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(txt);
if (m.find())
{
String ipaddress1=m.group(1);
String unixpath1=m.group(2);
System.out.print("("+ipaddress1.toString()+")"+"("+unixpath1.toString()+")"+" ");
}
}
}
Sample Run:
(10.223.157.186)(/assets/js/lowpro.js)
Note: If you have any doubt, please do comment below. If you liked the code then please do like the solution.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.