import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:(?=(?&V_Obj))\\{|(?!^)\\G(?&Sep_Obj)\\s*)(?:(?&V_KeyVal)(?&Sep_Obj))*?\\s*(?&Str)\\s*:\\s*\\K(?&Numb)(?(DEFINE)(?<Sep_Ary>\\s*(?:,(?!\\s*[\\}\\]])|(?=\\])))(?<Sep_Obj>\\s*(?:,(?!\\s*[\\}\\]])|(?=\\})))(?<Er_Obj>(?>\\{(?:\\s*(?&Str)(?:\\s*:(?:\\s*(?:(?&Er_Value)|(?<Er_Ary>\\[(?:\\s*(?:(?&Er_Value)|(?&Er_Ary)|(?&Er_Obj))(?:(?&Sep_Ary)|(*ACCEPT)))*(?:\\s*\\]|(*ACCEPT)))|(?&Er_Obj))(?:(?&Sep_Obj)|(*ACCEPT))|(*ACCEPT))|(*ACCEPT)))*(?:\\s*\\}|(*ACCEPT))))(?<Er_Value>(?>(?&Numb)|(?>true|false|null)|(?&Str)))(?<Str>(?>\"[^\\\\\"]*(?:\\\\[\\s\\S][^\\\\\"]*)*\"))(?<Numb>(?>[+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][+-]?\\d+)?|(?:[eE][+-]?\\d+)))(?<V_KeyVal>(?>\\s*(?&Str)\\s*:\\s*(?&V_Value)\\s*))(?<V_Value>(?>(?&Numb)|(?>true|false|null)|(?&Str)|(?&V_Obj)|(?&V_Ary)))(?<V_Ary>\\[(?>\\s*(?&V_Value)(?&Sep_Ary))*\\s*\\])(?<V_Obj>\\{(?>(?&V_KeyVal)(?&Sep_Obj))*\\s*\\}))";
final String string = "{\n"
+ " \"first_name\":\"sample\",\n"
+ " \"last_name\": \"lastname\",\n"
+ " \"integer\" : 100,\n"
+ " \"float\" : 1555.20,\n"
+ " \"createddate\":\"2015-06-25 09:57:28\",\n"
+ " \"asder\" : \n"
+ " [ \n"
+ " 200, \n"
+ " 100,\n"
+ " {\n"
+ " \"nother1\" : \"here\",\n"
+ " \"digi1\" : 900e10,\n"
+ " \"nother1\" : \"here\",\n"
+ " \"digi2\" : 3.14\n"
+ " },\n"
+ " 400,\n"
+ " 37\n"
+ " ]\n"
+ "}";
final String subst = "\"$0\"";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
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