이 클래스는 apache access로그를 regular expression으로 파싱할 수 있습니다.
This class can parse the apache access log using Java regular expression.
package test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExTest {
/**
* @param args
*/
public static void main(String[] args) {
String txt = "127.0.0.1 - - [25/Aug/2010:15:40:26 +0900] \"GET /favicon.ico HTTP/1.1\" 404 209";
String re1 = "^([\\d.]+)"; // IP Part
String re2 = " (\\S+)"; // Non whitespace character(-)
String re3 = " (\\S+)"; // Non whitespace character(-)
String re4 = " \\[([\\w:/]+\\s[+\\-]\\d{4})\\]"; // Date
String re5 = " \"(.+?)\""; // Method and URL
String re6 = " (\\d{3})"; // return code
String re7 = " (\\d+)"; // response bytes
// 브라우저 유형, Caller의 정보 등은 별 필요없음.
Pattern p = Pattern.compile(re1 + re2 + re3 + re4 + re5 + re6 + re7, Pattern.CASE_INSENSITIVE
| Pattern.DOTALL);
Matcher m = p.matcher(txt);
int i = 1;
if (m.find()) {
System.out.print("(" + m.group(i++) + ")" + "\n");
System.out.print("(" + m.group(i++) + ")" + "\n");
System.out.print("(" + m.group(i++) + ")" + "\n");
System.out.print("(" + m.group(i++) + ")" + "\n");
System.out.print("(" + m.group(i++) + ")" + "\n");
System.out.print("(" + m.group(i++) + ")" + "\n");
System.out.print("(" + m.group(i++) + ")" + "\n");
}
}
}
http://www.javapattern.info/trackback/356





