import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^ ([\\w-_ ]+):\\n\\n +Type: AirPort\\n +Hardware.+\\n +BSD Device Name: (.+)\\n +IPv4 Addresses: (192[\\.\\d]+)$";
final String string = "ETHERNET DISCONNECTED, WI-FI RUNNING\n"
+ "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n\n\n"
+ "Network:\n\n"
+ " Thunderbolt Ethernet Slot 1:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: en8\n"
+ " IPv4:\n"
+ " Configuration Method: DHCP\n"
+ " IPv6:\n"
+ " Configuration Method: Automatic\n"
+ " Ethernet:\n"
+ " MAC Address: 00:50:b6:ca:f0:dc\n"
+ " Media Options: \n"
+ " Media Subtype: Auto Select\n"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 0\n\n"
+ " Thunderbolt Ethernet:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: en6\n"
+ " IPv4:\n"
+ " Configuration Method: DHCP\n"
+ " IPv6:\n"
+ " Configuration Method: Automatic\n"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 1\n\n"
+ " Wi-Fi:\n\n"
+ " Type: AirPort\n"
+ " Hardware: AirPort\n"
+ " BSD Device Name: en0\n"
+ " IPv4 Addresses: 192.168.1.16\n"
+ " IPv4:\n"
+ " AdditionalRoutes:\n"
+ " DestinationAddress: 192.168.1.16\n"
+ " SubnetMask: 255.255.255.255\n"
+ " DestinationAddress: 169.254.0.0\n"
+ " SubnetMask: 255.255.0.0\n"
+ " Addresses: 192.168.1.16\n"
+ " ARPResolvedHardwareAddress: 2c:b0:5d:25:af:69\n"
+ " ARPResolvedIPAddress: 192.168.1.1\n"
+ " Configuration Method: DHCP\n"
+ " ConfirmedInterfaceName: en0\n"
+ " Interface Name: en0\n"
+ " Network Signature: IPv4.Router=192.168.1.1;IPv4.RouterHardwareAddress=2c:b0:5d:25:af:69\n"
+ " Router: 192.168.1.1\n"
+ " Subnet Masks: 255.255.255.0\n"
+ " IPv6:\n"
+ " Configuration Method: Automatic\n"
+ " DNS:\n"
+ " Server Addresses: 192.168.1.1\n"
+ " DHCP Server Responses:\n"
+ " Domain Name Servers: 192.168.1.1\n"
+ " Lease Duration (seconds): 0\n"
+ " DHCP Message Type: 0x05\n"
+ " Routers: 192.168.1.1\n"
+ " Server Identifier: 192.168.1.1\n"
+ " Subnet Mask: 255.255.255.0\n"
+ " Ethernet:\n"
+ " MAC Address: 28:cf:e9:17:e0:d1\n"
+ " Media Options: \n"
+ " Media Subtype: Auto Select\n"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 2\n\n"
+ " USB Ethernet:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: en3\n"
+ " IPv4:\n"
+ " Configuration Method: DHCP\n"
+ " IPv6:\n"
+ " Configuration Method: Automatic\n"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 3\n\n"
+ " AX88179 USB 3.0 to Gigabit Ethernet:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: en7\n"
+ " IPv4:\n"
+ " Configuration Method: DHCP\n"
+ " IPv6:\n"
+ " Configuration Method: Automatic\n"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 4\n\n"
+ " Thunderbolt FireWire:\n\n"
+ " Type: FireWire\n"
+ " Hardware: FireWire\n"
+ " BSD Device Name: fw0\n"
+ " IPv4:\n"
+ " Configuration Method: DHCP\n"
+ " IPv6:\n"
+ " Configuration Method: Automatic\n"
+ " Ethernet:\n"
+ " MAC Address: 00:50:b6:10:00:00:5d:31\n"
+ " Media Options: Full Duplex\n"
+ " Media Subtype: Auto Select\n"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 5\n\n"
+ " iPhone:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: en5\n"
+ " IPv4:\n"
+ " Configuration Method: DHCP\n"
+ " IPv6:\n"
+ " Configuration Method: Automatic\n"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 6\n\n"
+ " Bluetooth PAN:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: en4\n"
+ " IPv4:\n"
+ " Configuration Method: DHCP\n"
+ " IPv6:\n"
+ " Configuration Method: Automatic\n"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 7\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