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: Ethernet\\n +Hardware.+\\n +BSD Device Name: (.+)\\n +IPv4 Addresses: (192[\\.\\d]+)$";
final String string = "Network:\n\n"
+ " USB 10/100/1000 LAN 2:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: en6\n"
+ " IPv4 Addresses: 192.168.136.251\n"
+ " IPv4:\n"
+ " AdditionalRoutes:\n"
+ " DestinationAddress: 192.168.136.251\n"
+ " SubnetMask: 255.255.255.255\n"
+ " DestinationAddress: 169.254.0.0\n"
+ " SubnetMask: 255.255.0.0\n"
+ " Addresses: 192.168.136.251\n"
+ " ARPResolvedHardwareAddress: f6:92:bf:88:c8:2e\n"
+ " ARPResolvedIPAddress: 192.168.136.1\n"
+ " Configuration Method: INFORM\n"
+ " ConfirmedInterfaceName: en6\n"
+ " Interface Name: en6\n"
+ " Network Signature: IPv4.Router=192.168.136.1;IPv4.RouterHardwareAddress=f6:92:bf:88:c8:2e\n"
+ " Router: 192.168.136.1\n"
+ " Subnet Masks: 255.255.255.255\n"
+ " IPv6:\n"
+ " Addresses: 2601:283:4200:d0a:42f:af72:ae41:3c8f, 2601:283:4200:d0a:bdb9:1223:3c59:f741, fd7b:2874:64bb:10:c0c:5dc1:be14:6d7d, fd7b:2874:64bb:10:a583:5011:61f6:cb0d\n"
+ " Configuration Method: Automatic\n"
+ " ConfirmedInterfaceName: en6\n"
+ " Interface Name: en6\n"
+ " Network Signature: IPv6.Prefix=2601:283:4200:d0a::/64;IPv6.RouterHardwareAddress=f6:92:bf:88:c8:2e\n"
+ " Prefix Length: 64, 64, 64, 64\n"
+ " Router: fe80::f492:bfff:fe88:c82e\n"
+ " DNS:\n"
+ " Server Addresses: 192.168.136.1, fd7b:2874:64bb:10::1\n"
+ " DHCP Server Responses:\n"
+ " Domain Name Servers: 192.168.136.1\n"
+ " DHCP Message Type: 0x05\n"
+ " Routers: 192.168.136.1\n"
+ " Server Identifier: 192.168.136.1\n"
+ " Ethernet:\n"
+ " MAC Address: 00:e0:4c:68:0f:96\n"
+ " Media Options: Full Duplex\n"
+ " Media Subtype: 1000baseT\n"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 0\n\n"
+ " USB 10/100/1000 LAN:\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: 1\n\n"
+ " USB Ethernet:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: en7\n"
+ " IPv4 Addresses: 10.10.10.6\n"
+ " IPv4:\n"
+ " Addresses: 10.10.10.6\n"
+ " Configuration Method: Manual\n"
+ " Subnet Masks: 255.255.0.0\n"
+ " IPv6:\n"
+ " Configuration Method: Automatic\n"
+ " DNS:\n"
+ " Server Addresses: 1.1.1.1, 1.0.0.1, 2606:4700:4700::1111, 2606:4700:4700::1111\n"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 2\n\n"
+ " USB 10/100/1000 LAN 3:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: en11\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"
+ " Wi-Fi:\n\n"
+ " Type: AirPort\n"
+ " Hardware: AirPort\n"
+ " BSD Device Name: en0\n"
+ " IPv4 Addresses: 192.168.136.184\n"
+ " IPv4:\n"
+ " AdditionalRoutes:\n"
+ " DestinationAddress: 192.168.136.184\n"
+ " SubnetMask: 255.255.255.255\n"
+ " DestinationAddress: 169.254.0.0\n"
+ " SubnetMask: 255.255.0.0\n"
+ " Addresses: 192.168.136.184\n"
+ " ARPResolvedHardwareAddress: f6:92:bf:88:c8:2e\n"
+ " ARPResolvedIPAddress: 192.168.136.1\n"
+ " Configuration Method: DHCP\n"
+ " ConfirmedInterfaceName: en0\n"
+ " Interface Name: en0\n"
+ " Network Signature: IPv4.Router=192.168.136.1;IPv4.RouterHardwareAddress=f6:92:bf:88:c8:2e\n"
+ " Router: 192.168.136.1\n"
+ " Subnet Masks: 255.255.255.0\n"
+ " IPv6:\n"
+ " Addresses: 2601:283:4200:d0a:1849:f70b:a649:aaa1, 2601:283:4200:d0a:7088:d9:fe7e:80e8, fd7b:2874:64bb:10:2d:2f19:470f:32d1, fd7b:2874:64bb:10:a12f:2f04:f8c:1cbb\n"
+ " Configuration Method: Automatic\n"
+ " ConfirmedInterfaceName: en0\n"
+ " Interface Name: en0\n"
+ " Network Signature: IPv6.Prefix=2601:283:4200:d0a::/64;IPv6.RouterHardwareAddress=f6:92:bf:88:c8:2e\n"
+ " Prefix Length: 64, 64, 64, 64\n"
+ " Router: fe80::f492:bfff:fe88:c82e\n"
+ " DNS:\n"
+ " Server Addresses: 192.168.136.1, fd7b:2874:64bb:10::1\n"
+ " DHCP Server Responses:\n"
+ " Domain Name Servers: 192.168.136.1\n"
+ " Lease Duration (seconds): 0\n"
+ " DHCP Message Type: 0x05\n"
+ " Routers: 192.168.136.1\n"
+ " Server Identifier: 192.168.136.1\n"
+ " Subnet Mask: 255.255.255.0\n"
+ " Ethernet:\n"
+ " MAC Address: f8:ff:c2:67:29:32\n"
+ " Media Options: \n"
+ " Media Subtype: Auto Select\n"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Sleep Proxies:\n"
+ " 1 m&m tv:\n"
+ " Marginal Power: 60\n"
+ " Metric: 703560\n"
+ " Portability: 35\n"
+ " Total Power: 63\n"
+ " Type: 70\n"
+ " 1 workout room:\n"
+ " Marginal Power: 60\n"
+ " Metric: 703560\n"
+ " Portability: 35\n"
+ " Total Power: 63\n"
+ " Type: 70\n"
+ " Service Order: 4\n\n"
+ " SparkFun Pro Micro:\n\n"
+ " Type: PPP (PPPSerial)\n"
+ " Hardware: Modem\n"
+ " BSD Device Name: usbmodem14131\n"
+ " IPv4:\n"
+ " Configuration Method: PPP\n"
+ " IPv6:\n"
+ " Configuration Method: Automatic\n"
+ " Proxies:\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 5\n\n"
+ " Display Ethernet:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: en9\n"
+ " IPv4 Addresses: 10.10.10.1\n"
+ " IPv4:\n"
+ " Addresses: 10.10.10.1\n"
+ " Configuration Method: Manual\n"
+ " Subnet Masks: 255.255.0.0\n"
+ " IPv6:\n"
+ " Configuration Method: Automatic\n"
+ " Ethernet:\n"
+ " MAC Address: 0c:4d:e9:ae:d3:b3\n"
+ " Media Options: \n"
+ " Media Subtype: none\n"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 6\n\n"
+ " Thunderbolt Ethernet:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: en12\n"
+ " IPv4 Addresses: 10.10.10.3\n"
+ " IPv4:\n"
+ " Addresses: 10.10.10.3\n"
+ " Configuration Method: Manual\n"
+ " Subnet Masks: 255.255.255.0\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\n"
+ " Thunderbolt Ethernet Slot 1, Port 1:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: en10\n"
+ " IPv4:\n"
+ " Configuration Method: DHCP\n"
+ " IPv6:\n"
+ " Configuration Method: Automatic\n"
+ " Ethernet:\n"
+ " MAC Address: 64:4b:f0:19:ac:9c\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: 8\n\n"
+ " Display 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:0a:27:02:00:5d:2a:82\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: 9\n\n"
+ " Bluetooth PAN 2:\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"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 10\n\n"
+ " Thunderbolt Bridge:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: bridge0\n"
+ " IPv4 Addresses: 10.10.10.2\n"
+ " IPv4:\n"
+ " Addresses: 10.10.10.2\n"
+ " Configuration Method: Manual\n"
+ " Subnet Masks: 255.0.0.0\n"
+ " IPv6:\n"
+ " Configuration Method: Link Local\n"
+ " Proxies:\n"
+ " Exceptions List: *.local, 169.254/16\n"
+ " FTP Passive Mode: Yes\n"
+ " Service Order: 11\n\n"
+ " Thunderbolt FireWire:\n\n"
+ " Type: FireWire\n"
+ " Hardware: FireWire\n"
+ " BSD Device Name: fw1\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: 12\n\n"
+ " Thunderbolt FireWire 2:\n\n"
+ " Type: FireWire\n"
+ " Hardware: FireWire\n"
+ " BSD Device Name: fw2\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: 13\n\n"
+ " Thunderbolt Bridge 2:\n\n"
+ " Type: Ethernet\n"
+ " Hardware: Ethernet\n"
+ " BSD Device Name: bridge0\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: 14\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