import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?<hostip>[^ ]+) - - \\[(?<timestamp>[^ ]+)\\s-(?<unknown1>\\d+)] \"(?<method>[^ ]+) (?<request>[^ ]+) HTTP\\/\\d\\.\\d\" (?<response>\\d+) - (?<bytes>\\d+)";
final String string = "10.170.63.247 - - [16/Jun/2015:10:25:56 -0400] \"GET /iconic/staticContentBuild/js/jquery-1.8.2.js HTTP/1.1\" 200 - 580 SMUSER=- remote-client-ip=10.29.110.51 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257\n\n"
+ "10.170.63.247 - - [16/Jun/2015:10:25:56 -0400] \"GET /retail/staticContentBuild/js/pos-module.js HTTP/1.1\" 200 - 120 SMUSER=- remote-client-ip=10.164.96.28 http_referer=- Agent=Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E)\n\n"
+ "10.170.63.247 - - [16/Jun/2015:10:25:56 -0400] \"GET /retail/mobilepos/screens/ETR2/styles/omnilink.css HTTP/1.1\" 200 - 170 SMUSER=- remote-client-ip=10.27.174.140 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257\n\n"
+ "10.170.63.247 - - [16/Jun/2015:10:25:57 -0400] \"GET /retail/staticContentBuild/images/queue_icon_greyw.png HTTP/1.1\" 200 - 230 SMUSER=- remote-client-ip=10.167.154.165 http_referer=- Agent=Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Tablet PC 2.0; .NET CLR 1.1.4322)\n\n"
+ "10.170.63.247 - - [16/Jun/2015:10:25:57 -0400] \"GET /retail/staticContentBuild/js/handlebar-data.js HTTP/1.1\" 200 - 140 SMUSER=- remote-client-ip=10.166.12.56 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257\n\n"
+ "10.144.62.247 - - [16/Jun/2015:10:25:57 -0400] \"GET /retail/mobilepos/screens/ETR2/scripts/templates/device-details/modals/reset-voicemail-password/resetVoicemailPasswordItemView.tmpl HTTP/1.1\" 200 - 180 SMUSER=- remote-client-ip=10.130.4.66 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257\n\n"
+ "10.170.63.247 - - [16/Jun/2015:10:25:57 -0400] \"GET /iconic/staticContentBuild/js/accountLines.js HTTP/1.1\" 200 - 240 SMUSER=- remote-client-ip=10.164.18.46 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257\n";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
if (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