import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^([^\\n]*TenGigE[^\\n]*)(?:(?!TenGigE|carrier-delay).)*([^\\n]*carrier-delay[^\\n]*)";
final String string = "interface TenGigE0/0/0/8\n"
+ " bundle id 221 mode active\n"
+ " lacp period short\n"
+ " lacp period short receive 100\n"
+ " lacp period short transmit 100\n"
+ " carrier-delay up 100 down 100\n\n"
+ "interface TenGigE0/0/0/7\n\n\n\n"
+ "shutdown\n\n"
+ "!\n\n"
+ "interface TenGigE0/0/0/8\n\n\n\n"
+ " bundle id 221 mode active\n\n"
+ " lacp period short\n\n"
+ " lacp period short receive 100\n\n"
+ " lacp period short transmit 100\n\n"
+ " carrier-delay up 100 down 100\n\n"
+ " load-interval 30\n\n"
+ " frequency synchronization\n\n"
+ " !\n\n"
+ " transceiver permit pid all\n\n"
+ "!\n\n"
+ "interface TenGigE0/0/0/9\n\n\n\n"
+ " mtu 9216\n\n"
+ " frequency synchronization\n\n"
+ " !\n\n"
+ " transceiver permit pid all\n\n"
+ "!\n\n"
+ "interface TenGigE0/0/0/10\n\n\n\n"
+ " bundle id 237 mode active\n\n"
+ " lacp period short\n\n"
+ " lacp period short receive 100\n\n"
+ " lacp period short transmit 100\n\n"
+ " carrier-delay up 120000 down 150\n\n"
+ " load-interval 30\n\n"
+ " frequency synchronization";
final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | 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