import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(<[\\s]*(?![\\/])figure.*?>)|(([^<.*figure.*>\\w])(<[\\s]*(?![\\/])img.*?>)([^<.*\\/*figure.*>]))";
final String string = "аываыва <img sdasd asd > sdf dsf < sdf imG>\n\n"
+ "<h2>\n\n"
+ "< H2 sdf > </h2> sdf sdf sdf sdfsdf sdf sd f\n\n"
+ "< sdf sdf H2 sdf > \n\n"
+ "</h2>\n"
+ "< h2> adasd asd < h2> \n"
+ "< / H2>\n\n\n"
+ "<img 2>\n\n"
+ "<figure><img></figure> ыва ыва ыва ыва ыва ыва\n\n"
+ "< figure >< img >< / figure >\n\n"
+ "< figure sdfsdfsdfsd f> asd as das asd< img asd asd a/> asd asd asd <> < / figure> фываы ва ыв\n\n\n"
+ "<img>\n\n"
+ "< figure > фыв ыфв фыв фыв ыфв < img > < / figure >\n\n"
+ "<figure itemprop=\"associatedMedia image\" itemscope=\"\" itemtype=\"https://schema.org/ImageObject\"> <meta itemprop=\"representativeOfPage\" content=\"true\"> <img itemprop=\"contentUrl url\" src=\"//site.com/url.jpg\" title=\"\" alt=\"\"> <meta itemprop=\"name\" content=\"\"> <meta itemprop=\"width\" content=\"\"> <meta itemprop=\"height\" content=\"\"></figure>";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
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