regex - java regular expression for parsing log file -
i trying parse log file, , want extract parameters lines entered. here example, 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 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 error java. error "regular expression not matching
", code, indicates reges bogus. basically, want extract date&time, hostname, program name, , log message problem @ extracting program name, first thing before first colon, example line above 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 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 appreciated!
it seems hostname , program name cannot contain spaces - knowing can simplify regexp lot: separate hostname, program name , log message using whitespace characters - , work:
final string logentrypattern = "(\\w+\\s+\\d+\\s+\\d{2}:\\d{2}:\\d{2})\\s+(\\s+)\\s+(\\s+):\\s+(.+)"; final pattern p = pattern.compile(logentrypattern); final 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));
Comments
Post a Comment