import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(,\\s*(*SKIP))?+\n"
+ "# Attempts to match a comma and advanced whitespaces,\n"
+ "# without backtracking;\n"
+ "# And if the comma is matched, use (*SKIP) verb,\n"
+ "# which advances the pointer if we fail to match the comma.\n\n"
+ "# Key - Value pairs not worthy of keeping.\n"
+ "(\n"
+ " \"(?!jobID\"|exec)[^\"]+\" # Check if we like this key.\n"
+ " \\s*+:\\s*+ # The colon, advance whitespaces.\n"
+ " ( # Check keys recursively.\n"
+ " \"[^\"]*\"\n"
+ " # String literals, boring.\n"
+ " | \\{(?2)?+(?>,\\s*(?2))*\\}\n"
+ " # Or: An object storing some key-value pairs\n"
+ " # we don't care about.\n"
+ " | \\[(?3)?+(?>,\\s*(?3))*\\]\n"
+ " # Or: An array storing some values\n"
+ " # we don't care about.\n"
+ " )\n"
+ ")\n"
+ "(?(1)|,?)\n"
+ "# Balance the comma (so the result string is still valid JSON)";
final String string = " \"2597401\":[{\"jobID\":\"2597401\",\n"
+ " \"account\":\"TG-CCR120014\",\n"
+ " \"user\":\"charngda\",\n"
+ " \"pkgT\":{\"pgi/7.2- 5\":{\"libA\":[\"libpgc.so\"],\n"
+ " \"flavor\":[\"default\"]}}, \n"
+ " \"startEpoch\":\"1338497979\",\n"
+ " \"runTime\":\"1022\",\n"
+ " \"execType\":\"user:binary\", \n"
+ " \"exec\":\"ft.D.64\",\n"
+ " \"numNodes\":\"4\",\n"
+ " \"sha1\":\"5a79879235aa31b6a46e73b43879428e2a175db5\",\n"
+ " \"execEpoch\":1336766742,\n"
+ " \"execModify\":\"Fri May 11 15:05:42 2012\",\n"
+ " \"startTime\":\"Thu May 31 15:59:39 2012\",\n"
+ " \"numCores\":\"64\",\n"
+ " \"sizeT\":{\"bss\":\"1881400168\",\"text\":\"239574\",\"data\":\"22504\"}}, \n"
+ " {\"jobID\":\"2597401\",\n"
+ " \"account\":\"TG-CCR120014\",\n"
+ " \"user\":\"charngda\",\n"
+ " \"pkgT\":{\"pgi/7.2-5\":{\"libA\":[\"libpgc.so\"],\n"
+ " \"flavor\":[\"default\"]}},\n"
+ " \"startEpoch\":\"1338497946\",\n"
+ " \"runTime\":\"33\" \"execType\":\"user:binary\",\n"
+ " \"exec\":\"cg.C.64\",\n"
+ " \"numNodes\":\"4\",\n"
+ " \"sha1\":\"caf415e011e28b7e4e5b050fb61cbf71a62a9789\",\n"
+ " \"execEpoch\":1336766735,\n"
+ " \"execModify\":\"Fri May 11 15:05:35 2012\",\n"
+ " \"startTime\":\"Thu May 31 15:59:06 2012\",\n"
+ " \"numCores\":\"64\",\n"
+ " \"sizeT\":{\"bss\":\"29630984\",\"text\":\"225749\",\"data\":\"20360\"}},\n"
+ " {\"jobID\":\"2597401\",\n"
+ " \"account\":\"TG-CCR120014\",\n"
+ " \"user\":\"charngda\",\n"
+ " \"pkgT\":{\"pgi/7.2-5\": {\"libA\":[\"libpgc.so\"],\n"
+ " \"flavor\":[\"default\"]}},\n"
+ " \"startEpoch\":\"1338500447\",\n"
+ " \"runTime\":\"145\",\n"
+ " \"execType\":\"user:binary\",\n"
+ " \"exec\":\"mg.D.64\",\n"
+ " \"numNodes\":\"4\",\n"
+ " \"sha1\":\"173de32e1514ad097b1c051ec49c4eb240f2001f\",\n"
+ " \"execEpoch\":1336766756,\n"
+ " \"execModify\":\"Fri May 11 15:05:56 2012\",\n"
+ " \"startTime\":\"Thu May 31 16:40:47 2012\",\n"
+ " \"numCores\":\"64\",\n"
+ " \"sizeT\":{\"bss\":\"456954120\",\"text\":\"426186\",\"data\":\"22184\"}},{\"jobID\":\"2597401\",\n"
+ " \"account\":\"TG-CCR120014\",\n"
+ " \"user\":\"charngda\",\n"
+ " \"pkgT\":{\"pgi/7.2-5\":{\"libA\":[\"libpgc.so\"],\n"
+ " \"flavor\":[\"default\"]}},\n"
+ " \"startEpoch\":\"1338499002\",\n"
+ " \"runTime\":\"1444\",\n"
+ " \"execType\":\"user:binary\",\n"
+ " \"exec\":\"lu.D.64\",\n"
+ " \"numNodes\":\"4\",\n"
+ " \"sha1\":\"c6dc16d25c2f23d2a3321d4feed16ab7e10c2cc1\",\n"
+ " \"execEpoch\":1336766748,\n"
+ " \"execModify\":\"Fri May 11 15:05:48 2012\",\n"
+ " \"startTime\":\"Thu May 31 16:16:42 2012\",\n"
+ " \"numCores\":\"64\",\n"
+ " \"sizeT\":{\"bss\":\"199850984\",\"text\":\"474218\",\"data\":\"27064\"}}],";
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