import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "addr=\\\"(([0-9]{1,3}[\\\\.]){3}[0-9]{1,3})\\\"";
final String string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<!DOCTYPE nmaprun>\n"
+ "<?xml-stylesheet href=\"file:///usr/bin/../share/nmap/nmap.xsl\" type=\"text/xsl\"?>\n"
+ "<!-- Nmap 7.80 scan initiated Fri Oct 16 04:52:18 2020 as: nmap -O -oX xml_data.xml 45.33.49.119 -->\n"
+ "<nmaprun scanner=\"nmap\" args=\"nmap -O -oX xml_data.xml 45.33.49.119\" start=\"1602823938\" startstr=\"Fri Oct 16 04:52:18 2020\" version=\"7.80\" xmloutputversion=\"1.04\">\n"
+ "<scaninfo type=\"syn\" protocol=\"tcp\" numservices=\"1000\" services=\"1,3-4,6-7,9,13,17,19-26,30,32-33,37,42-43,49,53,70,79-85,88-90,99-100,106,109-111,113,119,125,135,139,143-144,146,161,163,179,199,211-212,22>\n"
+ "<verbose level=\"0\"/>\n"
+ "<debugging level=\"0\"/>\n"
+ "<host starttime=\"1602823938\" endtime=\"1602823972\"><status state=\"up\" reason=\"reset\" reason_ttl=\"64\"/>\n"
+ "<address addr=\"45.33.49.119\" addrtype=\"ipv4\"/>\n"
+ "<hostnames>\n"
+ "<hostname name=\"ack.nmap.org\" type=\"PTR\"/>\n"
+ "</hostnames>\n"
+ "<ports><extraports state=\"filtered\" count=\"993\">\n"
+ "<extrareasons reason=\"no-responses\" count=\"993\"/>\n"
+ "</extraports>\n"
+ "<port protocol=\"tcp\" portid=\"22\"><state state=\"open\" reason=\"syn-ack\" reason_ttl=\"51\"/><service name=\"ssh\" method=\"table\" conf=\"3\"/></port>\n"
+ "<port protocol=\"tcp\" portid=\"25\"><state state=\"open\" reason=\"syn-ack\" reason_ttl=\"51\"/><service name=\"smtp\" method=\"table\" conf=\"3\"/></port>\n"
+ "<port protocol=\"tcp\" portid=\"70\"><state state=\"closed\" reason=\"reset\" reason_ttl=\"51\"/><service name=\"gopher\" method=\"table\" conf=\"3\"/></port>\n"
+ "<port protocol=\"tcp\" portid=\"80\"><state state=\"open\" reason=\"syn-ack\" reason_ttl=\"50\"/><service name=\"http\" method=\"table\" conf=\"3\"/></port>\n"
+ "<port protocol=\"tcp\" portid=\"113\"><state state=\"closed\" reason=\"reset\" reason_ttl=\"51\"/><service name=\"ident\" method=\"table\" conf=\"3\"/></port>\n"
+ "<port protocol=\"tcp\" portid=\"443\"><state state=\"open\" reason=\"syn-ack\" reason_ttl=\"50\"/><service name=\"https\" method=\"table\" conf=\"3\"/></port>\n"
+ "<port protocol=\"tcp\" portid=\"31337\"><state state=\"closed\" reason=\"reset\" reason_ttl=\"51\"/><service name=\"Elite\" method=\"table\" conf=\"3\"/></port>\n"
+ "</ports>\n"
+ "<os><portused state=\"open\" proto=\"tcp\" portid=\"22\"/>\n"
+ "<portused state=\"closed\" proto=\"tcp\" portid=\"70\"/>\n"
+ "<osmatch name=\"HP P2000 G3 NAS device\" accuracy=\"91\" line=\"34647\">\n"
+ "<osclass type=\"storage-misc\" vendor=\"HP\" osfamily=\"embedded\" accuracy=\"91\"><cpe>cpe:/h:hp:p2000_g3</cpe></osclass>\n"
+ "</osmatch>\n"
+ "<osmatch name=\"Linux 2.6.32\" accuracy=\"90\" line=\"55409\">\n"
+ "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"2.6.X\" accuracy=\"90\"><cpe>cpe:/o:linux:linux_kernel:2.6.32</cpe></osclass>\n"
+ "</osmatch>\n"
+ "<osmatch name=\"Linux 2.6.32 - 3.1\" accuracy=\"90\" line=\"56315\">\n"
+ "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"2.6.X\" accuracy=\"90\"><cpe>cpe:/o:linux:linux_kernel:2.6</cpe></osclass>\n"
+ "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"3.X\" accuracy=\"90\"><cpe>cpe:/o:linux:linux_kernel:3</cpe></osclass>\n"
+ "</osmatch>\n"
+ "<osmatch name=\"Ubiquiti AirMax NanoStation WAP (Linux 2.6.32)\" accuracy=\"90\" line=\"61488\">\n"
+ "<osclass type=\"WAP\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"2.6.X\" accuracy=\"90\"><cpe>cpe:/o:linux:linux_kernel:2.6.32</cpe></osclass>\n"
+ "<osclass type=\"WAP\" vendor=\"Ubiquiti\" osfamily=\"embedded\" accuracy=\"90\"><cpe>cpe:/h:ubnt:airmax_nanostation</cpe></osclass>\n"
+ "</osmatch>\n"
+ "<osmatch name=\"Linux 3.7\" accuracy=\"90\" line=\"65676\">\n"
+ "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"3.X\" accuracy=\"90\"><cpe>cpe:/o:linux:linux_kernel:3.7</cpe></osclass>\n"
+ "</osmatch>\n"
+ "<osmatch name=\"Linux 2.6.32 - 3.13\" accuracy=\"89\" line=\"56411\">\n"
+ "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"2.6.X\" accuracy=\"89\"><cpe>cpe:/o:linux:linux_kernel:2.6</cpe></osclass>\n"
+ "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"3.X\" accuracy=\"89\"><cpe>cpe:/o:linux:linux_kernel:3</cpe></osclass>\n"
+ "</osmatch>\n"
+ "<osmatch name=\"Linux 3.0 - 3.2\" accuracy=\"89\" line=\"62456\">\n"
+ "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"3.X\" accuracy=\"89\"><cpe>cpe:/o:linux:linux_kernel:3</cpe></osclass>\n"
+ "</osmatch>\n"
+ "<osmatch name=\"Linux 3.3\" accuracy=\"89\" line=\"65197\">\n"
+ "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"3.X\" accuracy=\"89\"><cpe>cpe:/o:linux:linux_kernel:3.3</cpe></osclass>\n"
+ "</osmatch>\n"
+ "<osmatch name=\"Infomir MAG-250 set-top box\" accuracy=\"89\" line=\"59437\">\n"
+ "<osclass type=\"media device\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"2.6.X\" accuracy=\"89\"><cpe>cpe:/o:linux:linux_kernel:2.6</cpe></osclass>\n"
+ "<osclass type=\"media device\" vendor=\"Infomir\" osfamily=\"embedded\" accuracy=\"89\"><cpe>cpe:/h:infomir:mag-250</cpe></osclass>\n"
+ "</osmatch>\n"
+ "<osmatch name=\"Ubiquiti Pico Station WAP (AirOS 5.2.6)\" accuracy=\"88\" line=\"102825\">\n"
+ "<osclass type=\"WAP\" vendor=\"Ubiquiti\" osfamily=\"AirOS\" osgen=\"5.X\" accuracy=\"88\"><cpe>cpe:/o:ubnt:airos:5.2.6</cpe></osclass>\n"
+ "</osmatch>\n"
+ "</os>\n"
+ "<uptime seconds=\"751775\" lastboot=\"Wed Oct 7 12:03:18 2020\"/>\n"
+ "<tcpsequence index=\"256\" difficulty=\"Good luck!\" values=\"5833A5A,D5206F79,8E66294D,8E3B0632,AC2DB44A,7F486476\"/>\n"
+ "<ipidsequence class=\"All zeros\" values=\"0,0,0,0,0,0\"/>\n"
+ "<tcptssequence class=\"1000HZ\" values=\"2CCF1F79,2CCF1FD4,2CCF2043,2CCF209E,2CCF2102,2CCF2164\"/>\n"
+ "<times srtt=\"193340\" rttvar=\"78390\" to=\"506900\"/>\n"
+ "</host>\n"
+ "<runstats><finished time=\"1602823973\" timestr=\"Fri Oct 16 04:52:53 2020\" elapsed=\"34.33\" summary=\"Nmap done at Fri Oct 16 04:52:53 2020; 1 IP address (1 host up) scanned in 34.33 seconds\" exit=\"success\"/><>\n"
+ "</runstats>\n"
+ "</nmaprun>";
final Pattern pattern = Pattern.compile(regex);
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