import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(datatype\\s+: 4\\b(?:(?!datatype\\s+: \\d)[\\s\\S])*?(maxlength\\s+:\\s0\\b))";
final String string = "field {\n"
+ " id : 536870914\n"
+ " name : Set Field (Submit Mode)\n"
+ " datatype : 4\n"
+ " fieldtype : 1\n"
+ " create-mode : 2\n"
+ " option : 2\n"
+ " timestamp : 1489159658\n"
+ " owner : John Smith\n"
+ " last-changed : John Smith\n"
+ " length-units : 0\n"
+ " maxlength : 0\n"
+ " clob-store-opt : 0\n"
+ " menu-style : 1\n"
+ " qbe-match-op : 1\n"
+ " fulltext-optns : 0\n"
+ " permission : 12\\1\n"
+ "}\n"
+ "field {\n"
+ " id : 536870915\n"
+ " name : Schema Name\n"
+ " datatype : 4\n"
+ " fieldtype : 1\n"
+ " create-mode : 2\n"
+ " option : 1\n"
+ " timestamp : 1165057260\n"
+ " owner : John Smith\n"
+ " last-changed : John Smith\n"
+ " length-units : 0\n"
+ " maxlength : 30\n"
+ " clob-store-opt : 0\n"
+ " menu-style : 1\n"
+ " qbe-match-op : 1\n"
+ " fulltext-optns : 0\n"
+ " permission : 12\\1\n"
+ "}\n"
+ "field {\n"
+ " id : 536870916\n"
+ " name : Type\n"
+ " datatype : 4\n"
+ " fieldtype : 1\n"
+ " create-mode : 2\n"
+ " option : 1\n"
+ " timestamp : 1165057260\n"
+ " owner : John Smith\n"
+ " last-changed : John Smith\n"
+ " length-units : 0\n"
+ " maxlength : 30\n"
+ " clob-store-opt : 0\n"
+ " menu-style : 2\n"
+ " qbe-match-op : 1\n"
+ " fulltext-optns : 0\n"
+ " permission : 12\\1\n"
+ "}\n"
+ "field {\n"
+ " id : 536870917\n"
+ " name : Set Field (Query Mode)\n"
+ " datatype : 4\n"
+ " fieldtype : 1\n"
+ " create-mode : 2\n"
+ " option : 2\n"
+ " timestamp : 1489159658\n"
+ " owner : John Smith\n"
+ " last-changed : John Smith\n"
+ " length-units : 0\n"
+ " maxlength : 0\n"
+ " clob-store-opt : 0\n"
+ " menu-style : 1\n"
+ " qbe-match-op : 1\n"
+ " fulltext-optns : 0\n"
+ " permission : 12\\1\n"
+ "}";
final Pattern pattern = Pattern.compile(regex);
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