You can do this as follows:
- Extracting TimeStamp
Search for the first occurrence of INFO
and then take the sub-string from 0
to the position - 1.
- Extracting Id & AnotherValueIWant
Search for the regex patter [0-9, ]
and then split the contents with ,
and take the 0
as Id
and 1
as AnotherValueIWant
.
Here is a quick code snippet:
public static void main (String[] args)
{
String sample = "2016-02-06 10:19:36,410 INFO [[RequestMappingHandlerMapping]]: I am an important log [123, 21] ...{.....}";
int position = sample.indexOf("INFO");
System.out.println("TimeStamp: " + sample.substring(0,position-1));
Pattern MY_PATTERN = Pattern.compile("\\[[0-9, ]+\\]");
Matcher m = MY_PATTERN.matcher(sample);
while (m.find()) {
String str = m.group(0);
String[] s = str.substring(1,str.length()-2).split(", ");
System.out.println("Id: " + s[0]);
System.out.println("AnotherValueIWant: " + s[1]);
}
}
Output:
TimeStamp: 2016-02-06 10:19:36,410
Id: 123
AnotherValueIWant: 2