Java Regular Expression match HTML <img> tags with attribute

Description

Java Regular Expression match HTML <img> tags with attribute


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=''/>";
        System.out.println(IsContainImg(html));
    }//from   ww w.j a va2s  .c  o m

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

    public static boolean IsContainImg(String html) {
        Matcher m = patternImg.matcher(html);
        while (m.find()) {
            return true;
        }
        return false;
    }
}



PreviousNext

Related