import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\\S+).*\\\"*(?:GET|POST|HEAD).* (MJ12bot|Baiduspider|AhrefsBot|UptimeRobot).*\"";
final String string = "69.162.111.222 - - [07/Feb/2017:15:54:14 +0200] \"HEAD / HTTP/1.1\" 200 296 \"http://dummydomain.nl/\" \"Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)\"\n"
+ "185.119.111.222 - - [07/Feb/2017:15:55:37 +0200] \"GET /wp-login.php HTTP/1.1\" 200 2860 \"-\" \"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36\"\n"
+ "185.119.111.222 - - [07/Feb/2017:15:55:37 +0200] \"POST /wp-login.php HTTP/1.1\" 200 3610 \"http://www.dummydomain.nl/wp-login.php\" \"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36\"\n"
+ "149.210.111.222 - - [07/Feb/2017:15:59:13 +0200] \"POST /wp-cron.php?doing_wp_cron=1496930353.4784278869628906250000 HTTP/1.1\" 200 181 \"http://www.dummydomain.nl/wp-cron.php?doing_wp_cron=1496930353.4784278869628906250000\" \"WordPress/4.7.5; http://www.dummydomain.nl\"\n"
+ "69.162.111.222 - - [07/Feb/2017:15:59:12 +0200] \"HEAD / HTTP/1.1\" 301 229 \"http://dummydomain.nl/\" \"Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)\"";
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