import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\"([^\"]+)\":";
final String string = "{\n"
+ " \"activity:status\": \"BOOKED\",\n"
+ " \"criteria:duration\": 8,\n"
+ " \"criteria:tripFrom\": 1500242400,\n"
+ " \"criteria:tripTo\": 1500933600,\n"
+ " \"intent:booker\": 0.06258322237017303,\n"
+ " \"intent:churnRisk\": 0.3004193725304727,\n"
+ " \"intent:churnRiskText\": \"LOW\",\n"
+ " \"intent:userClass\": \"CUSTOMER\",\n"
+ " \"issues:acs\": 14,\n"
+ " \"issues:total\": 0,\n"
+ " \"revenue\": 2896,\n"
+ " \"tracking:events\": 162,\n"
+ " \"tracking:firstVisit\": 1475320136,\n"
+ " \"tracking:lastVisit\": 1498054362,\n"
+ " \"tracking:sessions30\": 19,\n"
+ " \"tracking:timeSpent\": 10603,\n"
+ " \"value:potentialRevenue\": {\n"
+ " \"mean\": 2880.5258186397987,\n"
+ " \"stddev\": 504.1184773012633,\n"
+ " \"weight\": 1,\n"
+ " \"confidence\": 1\n"
+ " },\n"
+ " \"criteria:occupancy\": {\n"
+ " \"adults\": 2,\n"
+ " \"children\": 2,\n"
+ " \"infants\": 0\n"
+ " }\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