* * 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 Withou T * 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 dis
Claimer. * 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 are provided by the AUTHOR and CONTRIBUTORS ' as are ' and ' ' as ' ' * and any EXPRESS OR implied warranties, Includin G, 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 LI Mited 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 T ORT (including negligence OR otherwise) * arising in no WAY out of the "use of" this SOFTWARE, even IF advised of the * P
Ossibility of SUCH DAMAGE. * Java, the Duke mascot, and all variants of sun ' s Java "steaming coffee * cup" logo are trademarks of Sun Microsyste Ms. Sun ' s, and 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 a
nd C + + is also gratefully acknowledged.
* * Import java.util.regex.*; /** * Common fields for Apache LoG Demo.
* * Interface Logexample {/** The number of fields that must is found./public static final int num_fields = 9; /** the sample log entry to be parsed. * * public static final String logentryline = "123.45.67.89--[27/oct/2000:09:27:09-0400] \" get/java/javaresources.ht ML http/1.0\ "10450 \"-\ "\" mozilla/4.6 [en] (X11; U OpenBSD 2.8 i386;
NAV) \ ""; }/** * Parse an Apache log file with Regular Expressions/public class Logregexp implements Logexample {public static void Main (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)); }
}
Turn from: http://www.java2s.com/Code/Java/Development-Class/ParseanApachelogfilewithRegularExpressions.htm