import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^var json = \\K\\{.*?\\}(?=;)";
final String string = "Survey\n"
+ " .StylesManager\n"
+ " .applyTheme(\"default\");\n\n"
+ "var json = {\n"
+ " questions: [\n"
+ " {\n"
+ " name: \"name\",\n"
+ " type: \"text\",\n"
+ " title: \"Please enter your name:\",\n"
+ " placeHolder: \"Jon Snow\",\n"
+ " isRequired: true\n"
+ " }, {\n"
+ " name: \"birthdate\",\n"
+ " type: \"text\",\n"
+ " inputType: \"date\",\n"
+ " title: \"Your birthdate:\",\n"
+ " isRequired: true\n"
+ " }, {\n"
+ " name: \"color\",\n"
+ " type: \"text\",\n"
+ " inputType: \"color\",\n"
+ " title: \"Your favorite color:\"\n"
+ " }, {\n"
+ " name: \"email\",\n"
+ " type: \"text\",\n"
+ " inputType: \"email\",\n"
+ " title: \"Your e-mail:\",\n"
+ " placeHolder: \"jon.snow@nightwatch.org\",\n"
+ " isRequired: true,\n"
+ " validators: [\n"
+ " {\n"
+ " type: \"email\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ "};\n\n"
+ "window.survey = new Survey.Model(json);\n\n"
+ "survey\n"
+ " .onComplete\n"
+ " .add(function (result) {\n"
+ " document\n"
+ " .querySelector('#surveyResult')\n"
+ " .innerHTML = \"result: \" + JSON.stringify(result.data);\n"
+ " });\n\n"
+ "$(\"#surveyElement\").Survey({model: survey});";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);
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