import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "os\\s*= (.*)";
final String string = "--- p0f 3.08b by Michal Zalewski <lcamtuf@coredump.cx> ---\n\n"
+ "[+] Closed 3 file descriptors.\n"
+ "[+] Loaded 324 signatures from '/etc/p0f/p0f.fp'.\n"
+ "[+] Will read pcap data from file 'temp.pcap'.\n"
+ "[+] Default packet filtering configured [+VLAN].\n"
+ "[+] Processing capture data.\n\n"
+ ".-[ 10.0.7.20/44964 -> 216.58.208.37/443 (syn) ]-\n"
+ "|\n"
+ "| client = 10.0.7.20/44964\n"
+ "| os = Linux 3.11 and newer\n"
+ "| dist = 0\n"
+ "| params = none\n"
+ "| raw_sig = 4:64+0:0:1460:mss*20,7:mss,sok,ts,nop,ws:df,id+:0\n"
+ "|\n"
+ "`----\n\n"
+ ".-[ 10.0.7.20/44964 -> 216.58.208.37/443 (mtu) ]-\n"
+ "|\n"
+ "| client = 10.0.7.20/44964\n"
+ "| link = Ethernet or modem\n"
+ "| raw_mtu = 1500\n"
+ "|\n"
+ "`----\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