import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?P<timestamp>[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}-[0-9]{4}) (?P<severity>[A-Z]) (?P<component>[A-Z]+) ?\\[ ?(?P<context>[a-zA-Z0-9 ]+)] (?<message>[^\\n]+)";
final String string = "MongoDB\n\n\n"
+ "2014-11-03T18:28:32.450-0500 I NETWORK [initandlisten] waiting for connections on port 27017\n\n"
+ " 2019-04-09T14:03:15.546-0400 I COMMAND [conn1] command test.items appName: \"MongoDB Shell\" command: aggregate { aggregate: \"items\", pipeline: [ { $match: { a: { $gte: 4.0 } } } ], cursor: {}, lsid: { id: UUID(\"042b423f-91bb-43bd-a378-4657ad4e0b35\") }, $db: \"test\" } planSummary: COLLSCAN cursorid:7185821004643502676 keysExamined:0 docsExamined:25000 hasSortStage:1 numYields:289 nreturned:101 reslen:1305014 locks:{ Global: { acquireCount: { r: 387 } }, Database: { acquireCount: { r: 387 } }, Collection: { acquireCount: { r: 387 } } } storage:{ data: { bytesRead: 6808584, timeReadingMicros: 4336 } } protocol:op_msg 34439ms\n\n"
+ "2014-11-03T18:28:32.450-0500\n\n\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html