Q:

How to get all html tags in order from html string in Java

<td valign="top" width="230">
<div>
<b><a href="http://www.cs.cornell.edu/johannes/">Johannes Gehrke</a></b>
</div>
<div class="small">
Professor<br>Computer Science, CS Field Member<br>Director of Graduate Studies<br>
Ph.D., Univ of Wisconsin, Madison, 1999<br><b>Research focus:</b> Database systems, data mining, and data privacy
</div>
</td>

I'd like to get the tag sequence from given html string. What's the easiest way to do it? For example, taking the above html string as input, I'd like my method to output an array of Strings, i.e.,[td,div,b,a,div,br,br,br,br,b].
I've tried Jsoup to parse the html string, but there seems no way to capture tags like br. I wonder any other Java libs can do the trick for me. The last resort would be using the regex technique. If someone can come up with a neat regular expression for that purpose, it will do as well.

A:

You can use regex:
    Pattern p = Pattern.compile("<([^\\s>/]+)");
    Matcher m = p.matcher(txt);
    while(m.find()) {
        String tag = m.group(1);
        System.out.println(tag);
    }

