import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "editor\\s*\\{[^\\{\\}\"]*\n\n"
+ "# Repeatedly match the key-value pairs\n"
+ "(?:\n"
+ " # Match from opening to closing double quote for the 'key'\n"
+ " \"[^\\\\\"]*\n"
+ " # Handle escaped characters: \\ followed by another character\n"
+ " (?: \\\\. [^\\\\\"]* )*\n"
+ " \"\n"
+ " # Then any whitespace between the 'key' and 'value'\n"
+ " \\s+\n"
+ " # Match from opening to closing double quote for the 'value'\n"
+ " \"[^\\\\\"]*\n"
+ " # Handle escaped characters: \\ followed by another character\n"
+ " (?: \\\\. [^\\\\\"]* )*\n"
+ " \"\n\n"
+ " # Match everything (especially whitespace) except these chars:\n"
+ " [^\\{\\}\"]*\n\n"
+ "|\n"
+ " # Key name may or may not be within quotes\n"
+ " \"? \\w+ \"? [^\\{\\}\"]*\n"
+ " \\{[^\\{\\}\"]*\n"
+ " (?:\n"
+ " \"[^\\\\\"]* (?: \\\\. [^\\\\\"]* )* \" \\s+ \"[^\\\\\"]* (?: \\\\. [^\\\\\"]* )* \"\n"
+ " [^\\{\\}\"]*\n"
+ " )*\n"
+ " \\} [^\\{\\}\"]*\n"
+ ")*\n\n"
+ "\\}";
final String string = "\"entity\"\n"
+ "{\n"
+ " \"id\" \"5040044\"\n"
+ " \"classname\" \"weapon_defibrillator_spawn\"\n"
+ " \"angles\" \"0 0 0\"\n"
+ " \"body\" \"0\"\n"
+ " \"disableshadows\" \"0\"\n"
+ " \"skin\" \"0\"\n"
+ " \"solid\" \"6\"\n"
+ " \"spawnflags\" \"3\"\n"
+ " \"origin\" \"449.47 5797.25 2856\"\n"
+ " editor\n"
+ " {\n"
+ " \"color\" \"0 0 200\"\n"
+ " \"visgroupshown\" \"1\"\n"
+ " \"visgroupautoshown\" \"1\"\n"
+ " \"logicalpos\" \"[-13268 14500]\"\n"
+ " \"helptext\" \"This is a \\\"help\\\" menu.\"\n"
+ " }\n"
+ " editor\n"
+ " {\n"
+ " \"color\" \"0 0 200\"\n"
+ " \"visgroupshown\" \"1\"\n"
+ " \"visgroupautoshown\" \"1\"\n"
+ " \"logicalpos\" \"[-13268 14500]\"\n"
+ " }\n"
+ " editor\n"
+ " {\n"
+ " \"color\" \"0 0 200\"\n"
+ " \"visgroupshown\" \"1\"\n"
+ " \"visgroupautoshown\" \"1\"\n"
+ " \"logicalpos\" \"[-13268 14500]\"\n"
+ " \"specialchars\" \"}\"\n"
+ " }\n"
+ " editor\n"
+ " {\n"
+ " \"child\" \"0 0 200\"\n"
+ " \"visgroupshown\" \"1\"\n"
+ " \"visgroupautoshown\" \"1\"\n"
+ " \"logicalpos\" \"[-13268 14500]\"\n"
+ " subgroup\n"
+ " {\n"
+ " \"child\" \"42 42 42\"\n"
+ " }\n"
+ " }\n"
+ "}\n";
final Pattern pattern = Pattern.compile(regex, 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