import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "([\\n^].*)(?=[\\n^]\\S+:|$)";
final String string = "ixl1: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500\n"
+ " options=e507bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,TSO6,LRO,VLAN_HWFILTER,VLAN_HWTSO,RXCSUM_IPV6,TXCSUM_IPV6>\n"
+ " ether f8:f2:1e:51:d8:fb\n"
+ " inet 100.0.0.196 netmask 0xff000000 broadcast 100.255.255.255\n"
+ " inet6 1234::196 prefixlen 64\n"
+ " inet6 fe80::faf2:1eff:fe51:d8fb%ixl1 prefixlen 64 scopeid 0x5\n"
+ " media: Ethernet autoselect (10Gbase-Twinax <full-duplex>)\n"
+ " status: active\n"
+ " nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>\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