0

Im trying to parse a log file and having a hard time trying to ignore a string within a token.

Part of the log I'm trying to parse:

[Wed Mar 06 20:56:27.121877 2019]

I want to create a token for the date where it should ignore any value after the second till the year and look like this:

Mar 06 20:56:27 2019

My regex string looks something like this at moment:

\[\S+ (\S+ \d+ \d+:\d+:\d+)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

3 Answers3

0

You could do something like this:

public class MyClass {
  public static void main(String args[]) {
    String test = "[Wed Mar 06 20:56:27.121877 2019]";
    String[] arr = test.split(" ");
    System.out.println(arr[1]+" "+arr[2]+" "+arr[3].substring(0,8)+" "+arr[4].substring(0,4));
  }
}

Output:

Mar 06 20:56:27 2019

F. Knorr
  • 3,045
  • 15
  • 22
0

You can try something like this :

public class Main {
  public static void main(String args[]) {
    String test = "[Wed Mar 06 20:56:27.121877 2019]";
    System.out.println(test.replaceAll("\\.[0-9]+|\\[|\\]", "").substring(test.indexOf(' ')));
  }
}

Output:

Mar 06 20:56:27 2019

Abhishek
  • 1,558
  • 16
  • 28
0

Start the regex from the month, ignore the Day. It would look similar to the below code snippet:

String dateform = "[Wed Mar 06 20:56:27.121877 2019]";
    String regex ="(\\S+ \\d+ \\d+:\\d+:\\d+)";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(dateform);
    if(m.find()){
       System.out.println(m.group(0));

    }

Output:

Mar 06 20:56:27

Ashwin
  • 41
  • 5