Parse Apache log file, regular expression __ regular expression

Source: Internet
Author: User
Tags apache log file apache log
Parse Apache log file, regular expression


/*
* Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
* All rights reserved. Software written by Ian F. Darwin and others.
* $Id: license,v 1.8 2004/02/09 03:33:38 Ian Exp $
*
* Redistribution and use in source and binary forms, with or without
* Modification, are permitted provided that following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* Notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* Notice, this list of conditions and the following disclaimer in the
* documentation and/or materials provided with the distribution.
*
* This SOFTWARE was provided by the AUTHOR and CONTRIBUTORS ' as is '
* and any EXPRESS OR implied warranties, including, BUT not LIMITED
* To, the implied warranties of merchantability and FITNESS for A particular
* Purpose ARE disclaimed. In NO EVENT SHALL the AUTHOR OR CONTRIBUTORS
* be liable to any DIRECT, INDIRECT, incidental, SPECIAL, exemplary, OR
* Consequential damages (including, BUT not LIMITED to, procurement of
* Substitute goods OR SERVICES; LOSS of Use, DATA, OR profits; OR BUSINESS
* interruption) HOWEVER caused and on any theory of liability, WHETHER in
* CONTRACT, STRICT liability, or TORT (including negligence OR otherwise)
* Arising in no WAY out of the "use of" this SOFTWARE, even IF advised of the
* Possibility of SUCH DAMAGE.
*
* Java, the Duke mascot, and all variants of Sun ' s Java ' steaming coffee
* Cup "logo are trademarks of Sun Microsystems. Sun ' s, James Gosling ' s,
* Pioneering role in inventing and promulgating (and standardizing) the Java
* Language and environment is gratefully acknowledged.
*
* The pioneering role is Dennis Ritchie and Bjarne Stroustrup, of At&t, for
* Inventing predecessor Languages C and C + + is also gratefully acknowledged.
*/
Importjava.util.regex.*;

/**
* Common fields for Apache Log demo.
*/
InterfaceLogexample {
/** the number of fields that must is found. */
Public static Final intNum_fields = 9;

/** the sample log entry to be parsed. */
Public static FinalString logentryline = "123.45.67.89--[27/oct/2000:09:27:09-0400]/" get/java/javaresources.html http/1.0/"200 10450 /"-/"/"mozilla/4.6 [en] (X11; U OpenBSD 2.8 i386; NAV)/"";

}


/**
* Parse an Apache log file with Regular Expressions
*/
Public classLogregexpImplementsLogexample {

Public Static voidMain (String argv []) {

String Logentrypattern = "^" ([//d.] +) (//s+) (//s+)//[([//w:/]+//s[+//-]//d{4})//]/"(. +?) /"(//d{3}) (//d+)/" ([^/]+)/"/" ([^/"]+")/"";

System.out.println ("Using RE pattern:");
System.out.println (Logentrypattern);

System.out.println ("Input line is:");
System.out.println (Logentryline);

Pattern p = pattern.compile (Logentrypattern);
Matcher Matcher = P.matcher (logentryline);
if(!matcher.matches () | |
Num_fields!= Matcher.groupcount ()) {
SYSTEM.ERR.PRINTLN ("Bad log entry (or problem with RE?):");
System.err.println (Logentryline);
return;
}
System.out.println ("IP Address:" + matcher.group (1));
System.out.println ("Date&time:" + matcher.group (4));
System.out.println ("Request:" + matcher.group (5));
System.out.println ("Response:" + matcher.group (6));
System.out.println ("Bytes Sent:" + matcher.group (7));
if(!matcher.group (8). Equals ("-"))
System.out.println ("Referer:" + matcher.group (8));
System.out.println ("Browser:" + matcher.group (9));
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.