import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "<p>DETAILS[\\s\\S]*|(<p>.*?<\\/p>)|.*";
final String string = "<info>\n"
+ "<owner>\n"
+ "<p>Owners:</p>\n"
+ "<p>1. New Owner_1</p>\n"
+ "<p>2. New Owner_2</p>\n"
+ "</owner>\n"
+ "<addons>\n"
+ "<p>Name of dog: Alex</p>\n"
+ "<p>1. Text blah blah blah</p>\n"
+ "<p>2. Text blah blah blah</p>\n"
+ "<p>3. Text blah blah blah</p>\n"
+ "<p>4. Text blah blah blah</p>\n"
+ "<p>OR MORE Text blah blah blah</p>\n"
+ "</addons>\n"
+ "<p>DETAILS</p>\n"
+ "<p>1. Vicky Mears 1st dog's owner.</p>\n"
+ "<p>2. Paul Nash 2nd dog's owner.</p>\n"
+ "<p>3. Dog found last Apr. 2016</p>\n"
+ "</info>";
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