JAVA: RegExp

Hi,
ich schreibe an einem Programm, dass die /var/log/vsftpd.log parst. Da es das erste Mal ist, dass ich mit RegExp arbeite, ist das ganz nich so einfach ;)

Beispiel-Zeile:
Sun Feb 25 12:33:28 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "221 Goodbye."

Alle Zeilen, haben 3 Variablen gemeinsam: das Datum , die PID, und die IP. Die folgende RegExp soll diese extrahieren:

Code:
String regexp = "^([^\\[]+?) \\[pid (\\d+?)\\] (.*) Client \"([\\d|\\.]*)\"(.*?)$";

Die Capture-Groups mögen nicht optimal sein, funktionieren aber soweit :D bis auf die letzte. Sie sollte eigentlich
einfangen. Tut sie aber nicht.

Hier der Code:
Code:
    private Action parseLine(String line) throws ParseException {       
        System.out.println("PARSING: " + line);
        
        //                 / DATE \          /PID\     /  \           /   IP   \    / \
        String regexp = "^([^\\[]+?) \\[pid (\\d+?)\\] (.*) Client \"([\\d|\\.]*)\"(.*?)$"; 
        
        //EXTRACT DATE, PID
        ArrayList<String> result    = findMatches(regexp, line);
        String dateString           = result.get(0);
        String pidString            = result.get(1);
        String ipString             = result.get(3);
        
        String toParse              = result.get(2);
        if(result.size() == 5) {
            System.out.println("adding index 4 ");
            toParse += result.get(4);
        }
        
        System.out.println("DATE    " + dateString);
        System.out.println("PID     " + pidString);
        System.out.println("IP      " + ipString);
        System.out.println("TOPARSE " + toParse);
        System.out.println();
        
        return null;
    }
    
    public static ArrayList<String> findMatches( String regexp, CharSequence s ) { 
      Pattern pattern = Pattern.compile(regexp);
      Matcher matcher = pattern.matcher(s);  
      matcher.find();
      
      ArrayList<String> results = new ArrayList<String>(); 
      for(int i=1; i<matcher.groupCount(); i++) {
          results.add(matcher.group(i));
      }

      return results; 
    }


Die Ausgabe sieht so aus:
PARSING: Sun Feb 25 12:33:28 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "221 Goodbye."
DATE Sun Feb 25 12:33:28 2007
PID 8625
IP 127.0.0.1
TOPARSE [mathias] FTP response:

Ihr seht ja, der letzte Teil fehlt einfach.

mfg
serow
 
Der Ausdruck sieht eigentlich ganz gut aus, lediglich der 4. Ausdruck ist ein bisschen Murks, denn die Pipe in einer Character Class ist keine entweder/oder-Auswahl wie in runden Klammern, sondern lediglich ein normales Zeichen. Das macht hier zwar nix aus, aber bevor's zur Gewohnheit wird, würde ich's rausnehmen. Würde das Fragezeichen aus der letzten Gruppe auch entfernen, auch wenn ich net denke, dass es daran liegt:

Code:
String regexp = "^([^\\[]+?) \\[pid (\\d+?)\\] (.*) Client \"([\\d\\.]*)\"(.*)$";
 
Ja stimmt, das mit dem ODER macht irgendwie keinen Sinn. Das änder aber leider nichts daran, dass der letzte Teil der Zeile einfach fehlt.

Ich poste jetzt mal die komplette Parser.java und das log file (nur ein paar Zeilen), in der Hoffnung, dass irgendwem irgendein Fehler auffällt.

Code:
/*
 * VSFTPDLogParser.java
 *
 * Created on 23. Februar 2007, 14:21
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package vsftpdlog.core;

import java.io.*;
import java.util.*;
import vsftpdlog.core.action.Action;
import vsftpdlog.core.User;
import vsftpdlog.core.*;
import java.text.*;
import java.util.regex.*;
import vsftpdlog.core.pool.ActionPool;
import vsftpdlog.core.pool.UserPool;

/**
 *
 * @author mathias
 */
public class Parser {
    
    private UserPool    users            = new UserPool();
    private ActionPool  actions          = new ActionPool();
    private String      logFileContent   = null;
    private File        logFile          = null;
    private long        fileSize         = 0;
           
    
    public void parse() throws IOException {
        System.out.println("parsing");
        if(logFile != null) {

            actions.clear();
            users.clear();
            
            BufferedReader br = new BufferedReader(new FileReader(logFile));
            StringBuffer buff = new StringBuffer();  
            String line;
            while( (line = br.readLine()) != null) {
                try {
                    buff.append(line + "\n");
                    parseLine(line);
                } catch(Exception ex) {
                    ex.printStackTrace();
                }
            }
            br.close();
            
            fileSize = logFile.length();
            logFileContent = buff.toString();
        }
    }
    private Action parseLine(String line) throws ParseException {       
        System.out.println("PARSING: " + line);
        
        //                 / DATE \          /PID\     /  \           /   IP  \   / \
        String regexp = "^([^\\[]+?) \\[pid (\\d+?)\\] (.*) Client \"([\\d\\.]*)\"(.*)$";
       
        //EXTRACT DATE, PID
        ArrayList<String> result    = findMatches(regexp, line);
        String dateString           = result.get(0);
        String pidString            = result.get(1);
        String ipString             = result.get(3);
        
        String toParse              = result.get(2);
        if(result.size() == 5) {
            System.out.println("adding index 4 ");
            toParse += result.get(4);
        }
        
        System.out.println("DATE    " + dateString);
        System.out.println("PID     " + pidString);
        System.out.println("IP      " + ipString);
        System.out.println("TOPARSE " + toParse);
        System.out.println();
        
        return null;
    }
    private ArrayList<String> findMatches( String regexp, CharSequence s ) { 
      Pattern pattern = Pattern.compile(regexp);
      Matcher matcher = pattern.matcher(s);  
      matcher.find();
      
      ArrayList<String> results = new ArrayList<String>(); 
      for(int i=1; i<matcher.groupCount(); i++) {
          results.add(matcher.group(i));
      }

      return results; 
    }


    public void setFile(File logFile) {
        this.logFile    = logFile;
        this.fileSize   = (long)0;
        this.actions.clear();
        this.users.clear(); 
        this.logFileContent = "";
    }
    public File getFile() {
        return logFile;
    }
        
    public String getFileContent() {
        return logFileContent;
    }
    public UserPool getUserPool() {
        return users;
    }
    public ActionPool getActionPool() {
        return actions;
    }

}


Code:
Sun Feb 25 12:33:22 2007 [pid 8624] CONNECT: Client "127.0.0.1"
Sun Feb 25 12:33:22 2007 [pid 8624] FTP response: Client "127.0.0.1", "220 (vsFTPd 2.0.4)"
Sun Feb 25 12:33:24 2007 [pid 8624] FTP command: Client "127.0.0.1", "AUTH SSL"
Sun Feb 25 12:33:24 2007 [pid 8624] FTP response: Client "127.0.0.1", "234 Proceed with negotiation."
Sun Feb 25 12:33:24 2007 [pid 8624] FTP command: Client "127.0.0.1", "USER mathias"
Sun Feb 25 12:33:24 2007 [pid 8624] [mathias] FTP response: Client "127.0.0.1", "331 Please specify the password."
Sun Feb 25 12:33:26 2007 [pid 8624] [mathias] FTP command: Client "127.0.0.1", "PASS <password>"
Sun Feb 25 12:33:26 2007 [pid 8623] [mathias] OK LOGIN: Client "127.0.0.1"
Sun Feb 25 12:33:26 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "230 Login successful."
Sun Feb 25 12:33:26 2007 [pid 8625] [mathias] FTP command: Client "127.0.0.1", "SYST"
Sun Feb 25 12:33:26 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "215 UNIX Type: L8"
Sun Feb 25 12:33:27 2007 [pid 8625] [mathias] FTP command: Client "127.0.0.1", "PORT 127,0,0,1,143,227"
Sun Feb 25 12:33:27 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "200 PORT command successful. Consider using PASV."
Sun Feb 25 12:33:27 2007 [pid 8625] [mathias] FTP command: Client "127.0.0.1", "LIST"
Sun Feb 25 12:33:27 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "150 Here comes the directory listing."
Sun Feb 25 12:33:27 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "226 Directory send OK."
Sun Feb 25 12:33:28 2007 [pid 8625] [mathias] FTP command: Client "127.0.0.1", "QUIT"
Sun Feb 25 12:33:28 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "221 Goodbye."


Ausgabe:
Code:
PARSING: Sun Feb 25 12:33:22 2007 [pid 8624] CONNECT: Client "127.0.0.1"
DATE    Sun Feb 25 12:33:22 2007
PID     8624
IP      127.0.0.1
TOPARSE CONNECT:

PARSING: Sun Feb 25 12:33:22 2007 [pid 8624] FTP response: Client "127.0.0.1", "220 (vsFTPd 2.0.4)"
DATE    Sun Feb 25 12:33:22 2007
PID     8624
IP      127.0.0.1
TOPARSE FTP response:

PARSING: Sun Feb 25 12:33:24 2007 [pid 8624] FTP command: Client "127.0.0.1", "AUTH SSL"
DATE    Sun Feb 25 12:33:24 2007
PID     8624
IP      127.0.0.1
TOPARSE FTP command:

PARSING: Sun Feb 25 12:33:24 2007 [pid 8624] FTP response: Client "127.0.0.1", "234 Proceed with negotiation."
DATE    Sun Feb 25 12:33:24 2007
PID     8624
IP      127.0.0.1
TOPARSE FTP response:

PARSING: Sun Feb 25 12:33:24 2007 [pid 8624] FTP command: Client "127.0.0.1", "USER mathias"
DATE    Sun Feb 25 12:33:24 2007
PID     8624
IP      127.0.0.1
TOPARSE FTP command:

PARSING: Sun Feb 25 12:33:24 2007 [pid 8624] [mathias] FTP response: Client "127.0.0.1", "331 Please specify the password."
DATE    Sun Feb 25 12:33:24 2007
PID     8624
IP      127.0.0.1
TOPARSE [mathias] FTP response:

PARSING: Sun Feb 25 12:33:26 2007 [pid 8624] [mathias] FTP command: Client "127.0.0.1", "PASS <password>"
DATE    Sun Feb 25 12:33:26 2007
PID     8624
IP      127.0.0.1
TOPARSE [mathias] FTP command:

PARSING: Sun Feb 25 12:33:26 2007 [pid 8623] [mathias] OK LOGIN: Client "127.0.0.1"
DATE    Sun Feb 25 12:33:26 2007
PID     8623
IP      127.0.0.1
TOPARSE [mathias] OK LOGIN:

PARSING: Sun Feb 25 12:33:26 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "230 Login successful."
DATE    Sun Feb 25 12:33:26 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP response:

PARSING: Sun Feb 25 12:33:26 2007 [pid 8625] [mathias] FTP command: Client "127.0.0.1", "SYST"
DATE    Sun Feb 25 12:33:26 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP command:

PARSING: Sun Feb 25 12:33:26 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "215 UNIX Type: L8"
DATE    Sun Feb 25 12:33:26 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP response:

PARSING: Sun Feb 25 12:33:27 2007 [pid 8625] [mathias] FTP command: Client "127.0.0.1", "PORT 127,0,0,1,143,227"
DATE    Sun Feb 25 12:33:27 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP command:

PARSING: Sun Feb 25 12:33:27 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "200 PORT command successful. Consider using PASV."
DATE    Sun Feb 25 12:33:27 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP response:

PARSING: Sun Feb 25 12:33:27 2007 [pid 8625] [mathias] FTP command: Client "127.0.0.1", "LIST"
DATE    Sun Feb 25 12:33:27 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP command:

PARSING: Sun Feb 25 12:33:27 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "150 Here comes the directory listing."
DATE    Sun Feb 25 12:33:27 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP response:

PARSING: Sun Feb 25 12:33:27 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "226 Directory send OK."
DATE    Sun Feb 25 12:33:27 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP response:

PARSING: Sun Feb 25 12:33:28 2007 [pid 8625] [mathias] FTP command: Client "127.0.0.1", "QUIT"
DATE    Sun Feb 25 12:33:28 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP command:

PARSING: Sun Feb 25 12:33:28 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "221 Goodbye."
DATE    Sun Feb 25 12:33:28 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP response:

PARSING: Sun Feb 25 12:33:22 2007 [pid 8624] CONNECT: Client "127.0.0.1"
DATE    Sun Feb 25 12:33:22 2007
PID     8624
IP      127.0.0.1
TOPARSE CONNECT:

PARSING: Sun Feb 25 12:33:22 2007 [pid 8624] FTP response: Client "127.0.0.1", "220 (vsFTPd 2.0.4)"
DATE    Sun Feb 25 12:33:22 2007
PID     8624
IP      127.0.0.1
TOPARSE FTP response:

PARSING: Sun Feb 25 12:33:24 2007 [pid 8624] FTP command: Client "127.0.0.1", "AUTH SSL"
DATE    Sun Feb 25 12:33:24 2007
PID     8624
IP      127.0.0.1
TOPARSE FTP command:

PARSING: Sun Feb 25 12:33:24 2007 [pid 8624] FTP response: Client "127.0.0.1", "234 Proceed with negotiation."
DATE    Sun Feb 25 12:33:24 2007
PID     8624
IP      127.0.0.1
TOPARSE FTP response:

PARSING: Sun Feb 25 12:33:24 2007 [pid 8624] FTP command: Client "127.0.0.1", "USER mathias"
DATE    Sun Feb 25 12:33:24 2007
PID     8624
IP      127.0.0.1
TOPARSE FTP command:

PARSING: Sun Feb 25 12:33:24 2007 [pid 8624] [mathias] FTP response: Client "127.0.0.1", "331 Please specify the password."
DATE    Sun Feb 25 12:33:24 2007
PID     8624
IP      127.0.0.1
TOPARSE [mathias] FTP response:

PARSING: Sun Feb 25 12:33:26 2007 [pid 8624] [mathias] FTP command: Client "127.0.0.1", "PASS <password>"
DATE    Sun Feb 25 12:33:26 2007
PID     8624
IP      127.0.0.1
TOPARSE [mathias] FTP command:

PARSING: Sun Feb 25 12:33:26 2007 [pid 8623] [mathias] OK LOGIN: Client "127.0.0.1"
DATE    Sun Feb 25 12:33:26 2007
PID     8623
IP      127.0.0.1
TOPARSE [mathias] OK LOGIN:

PARSING: Sun Feb 25 12:33:26 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "230 Login successful."
DATE    Sun Feb 25 12:33:26 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP response:

PARSING: Sun Feb 25 12:33:26 2007 [pid 8625] [mathias] FTP command: Client "127.0.0.1", "SYST"
DATE    Sun Feb 25 12:33:26 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP command:

PARSING: Sun Feb 25 12:33:26 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "215 UNIX Type: L8"
DATE    Sun Feb 25 12:33:26 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP response:

PARSING: Sun Feb 25 12:33:27 2007 [pid 8625] [mathias] FTP command: Client "127.0.0.1", "PORT 127,0,0,1,143,227"
DATE    Sun Feb 25 12:33:27 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP command:

PARSING: Sun Feb 25 12:33:27 2007 [pid 8625] [mathias] FTP response: Client "127.0.0.1", "200 PORT command successful. Consider using PASV."
DATE    Sun Feb 25 12:33:27 2007
PID     8625
IP      127.0.0.1
TOPARSE [mathias] FTP response:

Hab mal ein Java-Forum noch mit drangehängt. Hab die Erfahrung gemacht, wenn hier im HaBo nicht nach 1-2 Tage die Lösung da ist, dann kommt sie auch nicht mehr ;) (Ich will damit das HaBo und seine User loben, weil alles normalerweise sehr schnell geht.)

Hier der Link: http://forum.javacore.de/viewtopic.php?p=25240#25240
 
Zurück
Oben