import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "<\\s*[a-z]*[^>]*>(.*?)<\\s*/\\s*[a-z]*>|<[\\w]*>(.*?)<[/\\w]*>";
final String string = "<body>\n\n"
+ "<div class=\"header\">\n"
+ " <h1>My Website</h1>\n"
+ " <p>A <b>responsive</b> website created by me.</p>\n"
+ "</div>\n\n"
+ "<div class=\"navbar\">\n"
+ " <a href=\"#\" class=\"active\">Home</a>\n"
+ " <a href=\"#\">Link</a>\n"
+ " <a href=\"#\">Link</a>\n"
+ " <a href=\"#\" class=\"right\">Link</a>\n"
+ "</div>\n\n"
+ "<div class=\"row\">\n"
+ " <div class=\"side\">\n"
+ " <h2>About Me</h2>\n"
+ " <h5>Photo of me:</h5>\n"
+ " <div class=\"fakeimg\" style=\"height:200px;\">Image</div>\n"
+ " <p>Some text about me in culpa qui officia deserunt mollit anim..</p>\n"
+ " <h3>More Text</h3>\n"
+ " <p>Lorem ipsum dolor sit ame.</p>\n"
+ " <div class=\"fakeimg\" style=\"height:60px;\">Image</div><br>\n"
+ " <div class=\"fakeimg\" style=\"height:60px;\">Image</div><br>\n"
+ " <div class=\"fakeimg\" style=\"height:60px;\">Image</div>\n"
+ " </div>\n"
+ " <div class=\"main\">\n"
+ " <h2>TITLE HEADING</h2>\n"
+ " <h5>Title description, Dec 7, 2017</h5>\n"
+ " <div class=\"fakeimg\" style=\"height:200px;\">Image</div>\n"
+ " <p>Some text..</p>\n"
+ " <p>Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>\n"
+ " <br>\n"
+ " <h2>TITLE HEADING</h2>\n"
+ " <h5>Title description, Sep 2, 2017</h5>\n"
+ " <div class=\"fakeimg\" style=\"height:200px;\">Image</div>\n"
+ " <p>Some text..</p>\n"
+ " <p>Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>\n"
+ " </div>\n"
+ "</div>\n\n"
+ "<div class=\"footer\">\n"
+ " <h2>Footer</h2>\n"
+ "</div>\n\n"
+ "</body>";
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