import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "PROMPT \\d+:\\n(?<value>(?:.+(?:\\n\\n|$))+)";
final String string = "PROMPT 1:\n"
+ "User: Why did the kangaroo bring a mattress to the comedy show?\n\n"
+ "Assistant: I'm not sure, why did the kangaroo bring a mattress to the comedy show?\n\n"
+ "User: Because it wanted to have a \"hop\"-in good time and \"bounce\" back with laughter!\n\n"
+ "PROMPT 2:\n"
+ "User: What do you call a group of musical elephants?\n\n"
+ "Assistant: I don't know, what do you call a group of musical elephants?\n\n"
+ "User: A \"trumpet\" ensemble, because they sure know how to \"trunk\"-ate their talent!\n\n"
+ "PROMPT 3:\n"
+ "User: What did the chicken say to the comedian?\n\n"
+ "Assistant: I don't know, what did the chicken say to the comedian?\n\n"
+ "User: \"You crack me up!\" Although, I guess that's their job too, cluck it up already!";
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