public class Main {
public static void main(String[] args) {
String input = "<p>This is a sample text with <img src=\"image1.jpg\"> an <img src=\"image2.jpg\"> image.</p>";
String output = input.replaceAll("<img[^>]*>|<\/p>|<p>", "");
System.out.println(output);
}
}
如上去除字符串里面的img标签 跟p标签
输出
This is a sample text with an image.