0

I am trying to parse a log file with a regex and I understand the first of pulling out the IP addressed but I am stuck on how to move beyond the rest of it for the log file. So to start parsing the rest do I just tack on the regex to parse out the date and etc? So i would 2nd element to be the second ip of 72.37.100.86. Then I would like to exclude the "- - -" and have the date be the 4th element along with "GET / HTTP/1.1:" to be the 8th index along with the status code of 200 to be the 9th index. Any help with this would be much appreciated in understanding what I need to do next.

package com.text.nginx_log_parser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExTester {


// Actual Entry : 10.10.100.151 - 72.37.100.86, 192.36.20.508 - - - [04/Jul/2016:12:50:06 +0000]  https https https "GET / HTTP/1.1" 200 20027 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36"
public static String logEntry = "10.10.100.151 - 72.37.100.86, 192.36.20.508 - - - [04/Jul/2016:12:50:06 +0000]  https https https \"GET / HTTP/1.1\" 200 20027 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36\"\r\n";

//public static String regex = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})";
//public static String regex = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})";
public static void main (String [] args){

    String regex = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\s*-*\\s*-*\\s*-*";
    regexChecker(regex, logEntry);
    regex = "\\[*\\]\\s.";
    regexChecker(regex, logEntry);
}

public static void regexChecker(String regex, String str){

    Pattern pattern = Pattern.compile(regex);

    Matcher matcher = pattern.matcher(logEntry);
    //String firstIP = matcher.group(0);
    //String secondIP = matcher.group();
    //String timestamp = 
    while(matcher.find()){
        System.out.println( matcher.group(0));
    }
  }
}
1
  • what output your expect from this string? Commented Jun 5, 2017 at 13:04

1 Answer 1

1

With the following regex:

(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[-\s]+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).+?\[(.+?)\].*?\"(.+?)\"\s(\d{3}).*$ 

you are looking at capture groups 1 through 5 as per this entry on regex101.com

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.