2017-06-05 75 views
0

我想解析一個正則表達式的日誌文件,我明白拉出IP尋址的第一個,但我堅持如何超越其餘部分的日誌文件。因此,開始分析剩下的東西,我只是在正則表達式上解析出日期等?所以我會第二個元素是72.37.100.86的第二個IP。然後,我想排除「 - - - 」並將日期作爲第4個元素以及「GET/HTTP/1.1:」作爲第8個索引以及狀態代碼200作爲第9個索引。任何有關這方面的幫助將非常感謝您瞭解我接下來要做的事情。試圖用正則表達式解析日誌文件

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)); 
    } 
    } 
} 
+0

什麼輸出你期望從這個字符串? –

回答

1

用下面的正則表達式:

(\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}).*$ 

你通過看着捕捉組按照this entry on regex101.com