import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<x_client_ip>.*?)\\t-\\t-\\t.*?\\t(?<http_status>.*?)\\t(?<content_length>.*?)\\t(?<referer>.*?)\\t(?<user_agent>.*?)\\t(?<method>.*?)\\t(?<uri>.*?)\\t(?<query_string>.*?)\\t(?<protocol>.*?)\\t(?<duration>.*?)\\t-\\t-\\t-\\t(?<id>.*)\\t(?<client_ip>\\d+\\.\\d+\\.\\d+\\.\\d+)\\t";
final String string = "111.111.11.11 - - [17/Mar/2017:05:49:45 -0400] 302 231 \"https://test.test.com/test/test/viewTest\" \"Mozilla/5.0 (Windows NT 10.0; WOW64) webWebKit/537.36 (KHTML, like Gecko) Web/56.0.2924.87 Safari/537.36\" GET \"/favicon.ico\" \"\" HTTP/1.1 5236 - - - RES58cbb1390a02287220001fc146464dds 111.211.111.111 ";
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