import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "<img[^>]+src=\"http:\\/\\/www\\.example\\.org\\/image_to_be_removed\\.jpg\"[^>]*\\>";
final String string = "<h1>Test article with HTML5 tags</h1>\n"
+ "<nav><a href=\"/link1/\">Link 1</a></nav>\n"
+ "<p>This is an example article. The article may or may not include html5 tags, images and other things.</p>\n"
+ "<img src=\"http://www.example.org/image_to_be_removed.jpg\">\n"
+ "<p>More example text.</p>";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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