使用下面的範例就可以取得 tag 清單:
Pattern p = Pattern.compile("<([^\\s>/]+)");
Matcher m = p.matcher(txt);
while(m.find()) {
String tag = m.group(1);
System.out.println(tag);
}
白名單的比對,可以用 set 效率會比較好,範例:
Set<String> set = new HashSet<String>();
set.add("Hello");
set.add("Worlds");
System.out.println("Set: " + set);
System.out.println("Does the Set contains 'Hello'? "
+ set.contains("Hello"));
// Check if the Set contains "Hi"
System.out.println("Does the Set contains 'Hi'? "
+ set.contains("Hi"));
contains 有的話 = true, 不存在 = false.
資料來源:How to get all html tags in order from html string in Java
https://stackoverflow.com/questions/13529394/how-to-get-all-html-tags-in-order-from-html-string-in-java