// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.regex.Pattern;

public class UnQuotedAttribs {
    private static Pattern p1 =
        Pattern.compile("(href|src)[ ]*=[ ]*([!#\\$%&\\(\\)\\+,\\-\\./0-9:;=\\?@\\[\\]\\^_`a-z\\{\\|\\}~]+)[>]", Pattern.CASE_INSENSITIVE);
    public static boolean isProblem1(String htmlfrag) {
        return p1.matcher(htmlfrag).matches();
    }
    private static Pattern p2 =
        Pattern.compile("(href|src)[ ]*=[ ]*(?!&quot;)([!#\\$%&\\(\\)\\+,\\-\\./0-9:;=\\?@\\[\\]\\^_`a-z\\{\\|\\}~]+)[>]", Pattern.CASE_INSENSITIVE); 
    public static boolean isProblem2(String htmlfrag) {
        return p2.matcher(htmlfrag).matches();
    }
    private static Pattern p3 =
        Pattern.compile("(href|src)\\s*=\\s*[^'\"]*\\s*>", Pattern.CASE_INSENSITIVE);
    public static boolean isProblem3(String htmlfrag) {
        return p3.matcher(htmlfrag).matches();
    }
    private static Pattern p4 =
        Pattern.compile("(href|src)\\s*=\\s*[^'\"]*\\s*>", Pattern.CASE_INSENSITIVE); 
    public static boolean isProblem4(String htmlfrag) {
        return p4.matcher(htmlfrag.replace("&quot;", "\"")).matches();
    }
    private static void test(String htmlfrag) {
        System.out.println(htmlfrag);
        System.out.println(isProblem1(htmlfrag));
        System.out.println(isProblem2(htmlfrag));
        System.out.println(isProblem3(htmlfrag));
        System.out.println(isProblem4(htmlfrag));
    }
    public static void main(String[] args) {
        test("href=\"foobar.html\">");
        test("href = \"foobar.html\" >");
        test("href='foobar.html'>");
        test("href = 'foobar.html' >");
        test("href=foobar.html>");
        test("href = foobar.html >");
        test("href=&quot;foobar.html&quot;>");
        test("href = &quot;foobar.html&quot; >");
    }
}
