import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^([\\w.]+\\.)\\s+.+\\sNS\\s.+\\1";
final String string = "piggyvoucher.com. 86400 IN SOA ns1.krystal.co.uk. admin.krystal.co.uk. (\n"
+ " 2014060600 ;Serial Number\n"
+ " 86400 ;refresh\n"
+ " 7200 ;retry\n"
+ " 3600000 ;expire\n"
+ " 86400 ;minimum\n"
+ " )\n\n"
+ "piggyvoucher.com. 86400 IN NS ns1.piggyvoucher.com.\n"
+ "piggyvoucher.com. 86400 IN NS ns2.krystal.co.uk.\n\n\n"
+ "piggyvoucher.com. 14400 IN A 77.72.0.110\n\n"
+ "localhost 14400 IN A 127.0.0.1\n\n\n"
+ "mail 14400 IN CNAME piggyvoucher.com.\n"
+ "www 14400 IN CNAME piggyvoucher.com.\n"
+ "ftp 14400 IN A 77.72.0.110\n"
+ "cpanel 14400 IN A 77.72.0.110";
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