Option 1: Only One Tag
If you have only one image tag, just match it: the match is your new string.
Pattern regex = Pattern.compile("<img[^>]+>");
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
String ReplacedString = regexMatcher.group();
}
Option 2: Multiple Tags
Use this regex:
<img[^>]+>|(.)
This problem is a classic case of the technique explained in this question to "regex-match a pattern, excluding..."
The left side of the alternation |
matches complete <img tags>
. We will ignore these matches. The right side matches and captures single characters to Group 1, and we know they are the right ones because they were not matched by the expression on the left.
This program shows how to use the regex (see the results at the bottom of the online demo):
String subject = "qwerty <img src=\"image.jpg\"> zxc";
Pattern regex = Pattern.compile("<img[^>]+>|(.)");
Matcher m = regex.matcher(subject);
StringBuffer b= new StringBuffer();
while (m.find()) {
if(m.group(1) != null) m.appendReplacement(b, "");
else m.appendReplacement(b, m.group(0));
}
m.appendTail(b);
String replaced = b.toString();
System.out.println(replaced);
Reference