import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?><p\\s*>\\s*(<img\\s+(?:\"[\\S\\s]*?\"|'[\\S\\s]*?'|[^>]?)+\\s*\\/?>)\\s*<\\/p\\s*>)|(?><(?:(?:(?:(script|style|object|embed|applet|noframes|noscript|noembed)(?:\\s+(?>\"[\\S\\s]*?\"|'[\\S\\s]*?'|(?:(?!\\/>)[^>])?)+)?\\s*>)[\\S\\s]*?<\\/\\2\\s*(?=>))|(?:\\/?[\\w:]+\\s*\\/?)|(?:[\\w:]+\\s+(?:\"[\\S\\s]*?\"|'[\\S\\s]*?'|[^>]?)+\\s*\\/?)|\\?[\\S\\s]*?\\?|(?:!(?:(?:DOCTYPE[\\S\\s]*?)|(?:\\[CDATA\\[[\\S\\s]*?\\]\\])|(?:--[\\S\\s]*?--)|(?:ATTLIST[\\S\\s]*?)|(?:ENTITY[\\S\\s]*?)|(?:ELEMENT[\\S\\s]*?))))>)(*SKIP)(*FAIL)";
final String string = "$example1 = '<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"
+ "$example2 = '<p><img alt=\"image.jpg\" data-entity-type=\"\" data-entity-uuid=\"\" height=\"349\" src=\"image.jpg\" width=\"620\" /></p>';\n"
+ "$example3 = '<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"
+ "======================================\n";
final String subst = "$1";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
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