import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "locks:(?<locks>\\{.*?\\}+[\\s*\\}]+)";
final String string = "[conn5695298] query entitydb_20014.chats query: { $query: { $or: [ { receiver_id: \"5b91eb3dfe860a5325533467\" }, { sender_id: \"5b91eb3dfe860a5325533467\" } ], send_time: { $gt: 0 } }, $orderby: { send_time: -1 } } planSummary: COLLSCAN, COLLSCAN ntoreturn:120 ntoskip:0 nscanned:0 nscannedObjects:2216688 scanAndOrder:1 keyUpdates:0 writeConflicts:0 numYields:18133 nreturned:0 reslen:20 locks:{ Global: { acquireCount: { r: 36268 }, acquireWaitCount: { r: 49 }, timeAcquiringMicros: { r: 8560 } }, MMAPV1Journal: { acquireCount: { r: 18134 }, acquireWaitCount: { r: 2 }, timeAcquiringMicros: { r: 554 } }, Database: { acquireCount: { r: 18134 } }, Collection: { acquireCount: { R: 18134 } } } 4547ms\n\n"
+ " planSummary: IXSCAN { host: 1, gid: 1, service_code: 1, name: 1, instance: 1 } cursorid:44314245057 keysExamined:20225 docsExamined:20050 cursorExhausted:1 numYields:158 nreturned:20050 reslen:1596954 locks:{ Global: { acquireCount: { r: 318 } }, Database: { acquireCount: { r: 159 } }, Collection: { acquireCount: { r: 159 } } } protocol:op_command 136ms\n\n"
+ " planSummary: IXSCAN { host: 1, gid: 1, service_code: 1, name: 1, instance: 1 } cursorid:44314245057 keysExamined:20225 docsExamined:20050 cursorExhausted:1 numYields:158 nreturned:20050 reslen:1596954 locks:{ Global: 5 }, Database: { acquireCount: { r: 159 } }} }} }}}} }}}}}}, Collection: { acquireCount: { r: 159 } } } protocol:op_command 136ms";
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