import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?ms)statusCode: (?<status_code>\\d+)";
final String string = "{ [-]\n\n"
+ " body: { [-]\n\n"
+ " message: [ [-]\n\n"
+ " { [-]\n\n"
+ " errorMessage: must have required property 'objectIds'\n\n"
+ " field: objectIds\n\n"
+ " }\n\n"
+ " { [-]\n\n"
+ " errorMessage: must be equal to one of the allowed values : [object1,object2]\n\n"
+ " field: objectType\n\n"
+ " }\n\n"
+ " statusCode: 400\n\n"
+ " type: BAD_REQUEST_ERROR\n\n"
+ " }\n\n"
+ " headers: { [-]\n\n"
+ " Access-Control-Allow-Origin: *\n\n"
+ " Content-Type: application/json\n\n"
+ " }\n\n"
+ " hostname: \n\n"
+ " level: 50\n\n"
+ " msg: republish error response\n\n"
+ " statusCode: 400\n\n"
+ " time: ****\n\n"
+ "}\n\n"
+ " \n\n"
+ "Event 2:500 Error\n\n"
+ "{ [-]\n\n"
+ " awsRequestId: \n\n"
+ " body: { [-]\n\n"
+ " message: Unexpected token “ in JSON at position 98\n\n"
+ " }\n\n"
+ " headers: { [-]\n\n"
+ " Access-Control-Allow-Origin: *\n\n"
+ " Content-Type: application/json\n\n"
+ " }\n\n"
+ " msg: reprocess error response\n\n"
+ " statusCode: 500\n\n"
+ " time: ***\n\n"
+ "}\n\n"
+ "Event 3:Success\n\n"
+ "{ [-]\n\n"
+ " awsRequestId: \n\n"
+ " body: { [-]\n\n"
+ " message: republish request has been submitted for [1] ids\n\n"
+ " }\n\n"
+ " headers: { [-]\n\n"
+ " Access-Control-Allow-Origin: *\n\n"
+ " Content-Type: application/json\n\n"
+ " }\n\n"
+ " }\n\n"
+ " headers: { [+]\n\n"
+ " }\n\n"
+ " msg: republish success response\n\n"
+ " statusCode: 200\n\n"
+ " time: ***\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