title: java regular expression for parsing log file
body: <p>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:</p>
  
  <pre><code>"Apr  8 07:13:10 kali gnome-screensaver-dialog: gkr-pam: unlocked login keyring"
  </code></pre>
  
  <p>The program gives me:</p>
  
  <pre><code>Date&amp;Time: Apr 11 00:06:30
  Hostname: kali
  Program Name: gnome-screensaver-dialog
  Log: gkr-pam: unlocked login keyring
  </code></pre>
  
  <p>But for the line: </p>
  
  <pre><code>"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"
  </code></pre>
  
  <p>I have an error from java. The error is "<code>Regular Expression not matching</code>", from my code, which indicates that my reges is bogus.
  Basically, I want to extract the Date&amp;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:</p>
  
  <pre><code>   Date&amp;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
  </code></pre>
  
  <p>Here is my partial java code:</p>
  
  <pre><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&amp;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();
          }
      }
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>