import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?(DEFINE)\n"
+ " (?<ips>[^()]+)\n"
+ " (?<incoming>Incoming\\ Interface \\ List)\n"
+ " (?<outgoing>Outgoing\\ Interface \\ List)\n"
+ " (?<end>^$|\\Z)\n"
+ ")\n"
+ "^\\((?P<ip>(?&ips))\\)\n"
+ "(?:(?!(?&incoming))[\\s\\S]+?)\n"
+ "(?&incoming)[\\r\\n]\n"
+ "(?P<in>(?!(?&outgoing))[\\s\\S]+?)\n"
+ "(?&outgoing)[\\r\\n]\n"
+ "(?P<out>(?!^$)[\\s\\S]+?)\n"
+ "(?&end)";
final String string = "(192.168.1.1,232.0.6.8) RPF nbr: 55.44.23.1 Flags: RPF\n"
+ " Up: 4w1d\n"
+ " Incoming Interface List\n"
+ " TenGigE0/0/0/1 Flags: A, Up: 4w1d\n"
+ " Outgoing Interface List\n"
+ " TenGigE0/0/0/10 Flags: A, Up: 4w1d\n\n"
+ "(192.168.55.3,232.0.10.69) RPF nbr: 66.76.44.130 Flags: RPF\n"
+ " Up: 4w1d\n"
+ " Incoming Interface List\n"
+ " TenGigE0/0/0/0 Flags: A, Up: 4w1d\n"
+ " TenGigE0/1/0/0 Flags: A, Up: 4w1d\n"
+ " TenGigE0/2/0/0 Flags: A, Up: 4w1d\n"
+ " Outgoing Interface List\n"
+ " TenGigE0/0/0/10 Flags: A, Up: 4w1d\n"
+ " TenGigE0/3/0/0 Flags: A, Up: 4w1d\n"
+ " TenGigE0/4/0/0 Flags: A, Up: 4w1d";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS);
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