Java Regex Matcher remove HTML <img> tag

Description

Java Regex Matcher remove HTML <img> tag


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) throws Exception {
        String html = "<img src=\"asdf\" onload=\"adsf\" adsf/>";
        System.out.println(ReplaceImgTag(html));
    }//from  w  w  w . ja  va2 s.  c  o  m

    static final Pattern patternImgSrc = Pattern.compile("<img(.+?)src=\"(.+?)\"(.+?)>");

    public static String ReplaceImgTag(String html) {
        Matcher m = patternImgSrc.matcher(html);
        while (m.find()) {
            html = m.replaceAll("image");
        }
        return html;
    }
}

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) throws Exception {
        String html = "<img src=\"asdf\" onload=\"adsf\" adsf/>";
        System.out.println(RemoveImgTag(html));
    }//from  w  ww . ja  v  a 2  s  .  com

    static final Pattern patternImg = Pattern.compile("<img(.+?)src=\"(.+?)\"(.+?)(onload=\"(.+?)\")?([^\"]+?)>");

    public static String RemoveImgTag(String html) {
        Matcher m = patternImg.matcher(html);
        while (m.find()) {
            html = m.replaceAll("");
        }
        return html;
    }
}



PreviousNext

Related