import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?:(?<network_srcIpv4>(?:[0-9]{1,3}\\.){3}[0-9]{1,3})|(?<network_srcIpv6>[:\\-0-9a-fA-F]+?)|(?<network_srcHost>.+?)) - (?:-|(?<user_username>.+)) \\[(?<time>.*)\\] \\\"(?<application_cmd>(?<application_http_method>[A-Z]+)\\s(?:(?<application_proto>.*?)://)?(?<network_fqdn>[^/]*?)(?:\\:(?<network_dstPort>\\d+))?(?<file_path>/.*?)?(?:\\?(?<application_http_queryString>.*?))?(?: HTTP/(?<application_http_version>[0-9\\.]+)?))\\\" (?<application_http_status>\\d+) (?<application_len>\\d+) (?:\"(?:-|(?<application_http_referrer>.*))\")? (?:\"(?:-|(?<application_http_userAgent>.*))\")$";
final String string = "218.1.111.50 - - [13/Mar/2005:10:36:12 -0500] \"GET http://www.yahoo.com/ HTTP/1.1\" 403 2898 \"-\" \"Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)\"\n"
+ "24.23.244.160 - - [13/Mar/2005:13:48:40 -0500] \"GET /scripts/..%25%35%63../winnt/system32/cmd.exe?/c+dir HTTP/1.0\" 404 1041 \"-\" \"-\"";
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