I am trying to parse a log file, and I want to extract parameters from the lines entered. Here is an example, for the line:
"Apr 8 07:13:10 kali gnome-screensaver-dialog: gkr-pam: unlocked login keyring"
The program gives me:
Date&Time: Apr 11 00:06:30
Hostname: kali
Program Name: gnome-screensaver-dialog
Log: gkr-pam: unlocked login keyring
But for the line:
"Apr 8 07:13:45 kali gnome-screensaver-dialog: pam_unix(gnome-screensaver:auth): authentication failure; logname= uid=0 euid=0 tty=:0.0 ruser= rhost= user=root"
I have an error from java. The error is "Regular Expression not matching", from my code, which indicates that my reges is bogus.
Basically, I want to extract the Date&Time, Hostname, Program Name, and Log Message
The problem is at extracting the Program Name, it is the first thing before the first colon, for example for the line above it should give me:
Date&Time: Apr 8 07:13:45
Hostname: kali
Program Name: gnome-screensaver-dialog
Log: pam_unix(gnome-screensaver:auth): authentication failure; logname= uid=0 euid=0 tty=:0.0 ruser= rhost= user=root
Here is my partial java code:
private class FileTailerListenerAdapter extends TailerListenerAdapter {
@Override
public void handle(String line) {
String logEntryPattern = "([\\w]+\\s[\\d]+\\s[\\d:]+) ([\\w]+) ([\\[\\]\\(\\)a-zA-Z0-9\\-]+)[?:] (.+)";
Pattern p = Pattern.compile(logEntryPattern);
Matcher matcher = p.matcher(line);
if (!matcher.matches()) {
System.err.println("Regular Expression not matching:");
System.err.println(line);
return;
}
System.out.println("Total groups: " + matcher.groupCount());
System.out.println("Date&Time: " + matcher.group(1));
System.out.println("Hostname: " + matcher.group(2));
System.out.println("Program Name: " + matcher.group(3));
System.out.println("Log: " + matcher.group(4));
System.out.println();
System.out.println();
}
}
Any help would be greatly appreciated!
"(\\w+\\s+\\d+\\s+[\\d:]+)\\s+(\\w+)\\s+([^:]+):\\s+(.*)",[^:]+matches all the things not a: