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:\\.\\- ]+)\\s+\\[[^\\]]+\\]\\s+\\[(?P<thread>\\d+)\\]\\s+\\[(?P<level>\\w+)\\]\\s+(?P<message>.*)$";
final String string = "2024-01-31 12:38:52.485 [2024-01-31 17:38:47 +0000] [320] [INFO] Booting worker with pid: 320\n"
+ "2024-01-31 12:38:52.485 [2024-01-31 17:38:47 +0000] [319] [INFO] Booting worker with pid: 319\n"
+ "2024-01-31 12:38:52.485 [2024-01-31 17:38:47 +0000] [318] [INFO] Booting worker with pid: 318\n"
+ "2024-01-31 12:38:52.485 [2024-01-31 17:38:47 +0000] [317] [INFO] Booting worker with pid: 317\n"
+ "2024-01-31 12:38:52.485 [2024-01-31 17:38:47 +0000] [316] [INFO] Booting worker with pid: 316";
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