import java.util.regex.Pattern;

class DetectHtml
{
	// adapted from re posted by Phil Haack and modified to match better
	public final static String tagStart=
		"\\<\\w+((\\s+\\w+(\\s*\\=\\s*(?:\".*?\"|'.*?'|[^'\"\\>\\s]+))?)+\\s*|\\s*)\\>";
	public final static String tagEnd=
        "\\</\\w+\\>";
	public final static String tagSelfClosing=
		"\\<\\w+((\\s+\\w+(\\s*\\=\\s*(?:\".*?\"|'.*?'|[^'\"\\>\\s]+))?)+\\s*|\\s*)/\\>";
	public final static String htmlEntity=
		"&[a-zA-Z][a-zA-Z0-9]+;";
	public final static Pattern htmlPattern=Pattern.compile(
	  "("+tagStart+".*"+tagEnd+")|("+tagSelfClosing+")|("+htmlEntity+")",
	  Pattern.DOTALL
	);

	public static boolean isHtml(String s) {
		boolean ret=false;
		if (s != null) {
			ret=htmlPattern.matcher(s).find();
		}
		return ret;
	}

	// test - you can delete me
	public static void main (String[] args) throws java.lang.Exception
	{
		String strIsNotHtml="If B<A then A>B & this is true";
		String strIsHtml="<span id=\"true\">If B&lt;A then A&gt;B &amp; this is true</span>";
		String strIsMultilineHtml="<a href=\"http://w...content-available-to-author-only...e.com/\">\nclick here\n</a>";
		String strBadEndTagOnly="</end>";
		String strBadStartTagOnly="<start>";
		String strEmptyBraces="These are used <> to denote an HTML tag.";
		String strTextWithEntities="This is an example of HTML&nbsp;escaped text";

		System.out.println(strIsNotHtml + " - " + (isHtml(strIsNotHtml) ? "IS HTML" : "IS NOT HTML"));
		System.out.println(strIsHtml + " - " + (isHtml(strIsHtml) ? "IS HTML" : "IS NOT HTML"));
		System.out.println(strIsMultilineHtml + " - " + (isHtml(strIsMultilineHtml) ? "IS HTML" : "IS NOT HTML"));
		System.out.println(strBadEndTagOnly + " - " + (isHtml(strBadEndTagOnly) ? "IS HTML" : "IS NOT HTML"));
		System.out.println(strBadStartTagOnly + " - " + (isHtml(strBadStartTagOnly) ? "IS HTML" : "IS NOT HTML"));
		System.out.println(strEmptyBraces + " - " + (isHtml(strEmptyBraces) ? "IS HTML" : "IS NOT HTML"));
		System.out.println(strTextWithEntities + " - " + (isHtml(strTextWithEntities) ? "IS HTML" : "IS NOT HTML"));

	}

}