import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?<time>[^ ]* {1,2}[^ ]* [^ ]*) (?<ident>[a-zA-Z0-9_\\/\\.\\-]*)(?:\\[(?<pid>[0-9]+)\\])?(?:[^\\:]*\\:)? \\[(?<time_server>[^\\]]*)\\] (?<host>[^ ]*) - (?<user>[^ ]*)\\\\\"(?<method>\\S+)(?: +(?<path>[^\\\"]*) +\\S*)?\" (?<code>[^ ]*) (?<size>[^ ]*) \\\\\"(?<referer>[^\\\"]*)\\\\\" \\\\\"(?<agent>[^\\\"]*)\\\\\"rt=(?<request_time>[^ ]*) uct=\\\\\"(?<upstream_connect_time>[^ ]*)\\\\\" uht=\\\\\"(?<upstream_header_time>[^ ]*)\\\\\" urt=\\\\\"(?<upstream_response_time>[^ ]*)\\\\\" uaddr=\\\\\"(?<upstream_addr>[^ ]*)\\\\\" x-forwarded-for=\\\\\"*(?<http_x_forwarded_for>.*)\\\\\"$";
final String string = "Jun 26 06:53:39 00f628825635[3156]: [26/Jun/2018:06:53:39 +0000] 10.66.44.27 - -\\\"GET /health_check HTTP/1.1\\\" 200 0 \\\"-\\\" \\\"ELB-HealthChecker/1.0\\\"rt=0.000 uct=\\\"-\\\" uht=\\\"-\\\" urt=\\\"-\\\" uaddr=\\\"-\\\" x-forwarded-for=\\\"-\\\"";
final Pattern pattern = Pattern.compile(regex);
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