import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "<p(?:[^>]*|\\r\\n|\\n)>(?:.*|\\r\\n|\\n)(<img(?:[^>]*|\\r\\n|\\n)>)(?:.*|\\r\\n|\\n)<\\/p>";
final String string = "<p data-entity-type=\"\" data-entity-uuid=\"\" style=\"text-align: center;\"><span><img alt=\"image.jpg\" data-entity-type=\"\" data-entity-uuid=\"\" height=\"349\" src=\"image.jpg\" width=\"620\" /><span title=\"Click and drag to resize\">•</span></span></p>\n\n"
+ "<p><img alt=\"image.jpg\" data-entity-type=\"\" data-entity-uuid=\"\" height=\"349\" src=\"image.jpg\" width=\"620\" /></p>\n\n"
+ "<html>\n"
+ "<head></head>\n"
+ "<body>\n"
+ "some text here...\n"
+ "<p><img alt=\"image.jpg\" data-entity-type=\"\" data-entity-uuid=\"\" height=\"349\" src=\"image.jpg\" width=\"620\" />\n"
+ "</p>\n"
+ "</body>\n"
+ "</html>'\n\n"
+ "// Wanted result : <html><head></head><body>some text here...<img alt=\"julie-bishop.jpg\" data-entity-type=\"\" data-entity-uuid=\"\" height=\"349\" src=\"/sites/default/files/inline-images/julie-bishop.jpg\" width=\"620\" /></body></html>\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