import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<date>\\d+\\-\\d+\\-\\d+).(?<time>\\d+:\\d+:\\d+).(?<timetaken>\\d+).(?<cip>\\d+\\.\\d+\\.\\d+\\.\\d+).(?<scstatus>\\d+).(?<scaction>\\S+).(?<scbytes>\\d+).(?<csbytes>\\d+).(?<csmethod>\\S+).(?<csurischeme>\\S+).(?<cshost>\\S+).(?<csuriport>\\d+).(?<csuripath>\\S+).(?<csuriquery>\\S+).(?<csusername>\\S+).(?<csauthgroup>\\S+).(?<shierarchy>\\S+).(?<ssuppliername>\\S+).(?<rscontenttype>\\S+).(?<csreferer>\\S+).\"?(?<cs_user_agent>.+)\"?.";
final String string = "2014-03-27 12:39:32 20 10.71.15.207 304 TCP_HIT 367 1470 GET http www.computerworld.com 80 /elqNow/elqFCS.js - - - - 23.196.74.53 application/x-javascript http://www.computerworld.com/s/article/9247206/Gameover_malware_takes_aim_at_Monster.com_and_CareerBuilder.com \"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)\" OBSERVED \"Technology/Internet\" - 163.252.254.201 23.44.202.53 52809\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