views:

117

answers:

4

I Have a question about finding html tags using Java and Regex.

I am using the code below to find all the tags in HTML, documentURL is obviously the HTML content.

The find method return true, meaning that it can find something in the HTML but the matches() method always return false and I am completly and utterly puzzled about this.

I refered to Java documentations too but could not find my answer.

What is the correct way of using Matcher ?

    Pattern keyLineContents = Pattern.compile("(<.*?>)");

    Matcher keyLineMatcher = keyLineContents.matcher(documentURL);

    boolean result = keyLineMatcher.find();

    boolean matchFound = keyLineMatcher.matches();

Doing something like this throws an exeption:

     String abc = keyLineMatcher.group(0);

Thanks.

+7  A: 

The correct way to loop through matches is:

Pattern p = Pattern.compile("<.*?>");
Matcher m = p.matcher(htmlString);
while (m.find()) {
  System.out.println(m.group());
}

That being said, regular expressions are an extremely poor method of parsing HTML. The reason comes down to this: regular expressions work well for parsing regular languages. HTML is a context free language. Where regular expressions fall down is for things like nested tags, using > inside attribute values and so on.

Use a dedicated HTML parser instead such as HTML Parser.

cletus
Thanks it works.:)I will use html parser later .
Elham
"I will use html parser later". That's what they all say ... :-)
Stephen C
A: 

What if someone wants to write his/her own HTML parser.

If not Regex then, What is the best way to do it ?

Raha
I'd post this as a separate question, if it hasn't already been asked. You might consider if you want a language-specific answer or a general parsing answer when phrasing the question.
TrueWill
Regex only solves the scanning part. Parsing is a completely different exercise. And as HTML has an nested (recursive) syntax, you need a parser, not just a scanner.
EJP
+2  A: 

Why don't you try looking at the source code of some open source HTML Parsers? HtmlCleaner, Tagsoup etc.

The general strategy seems to be to attempt to parse and clean the html and return an Xml tree.

Personally, I would read through the HTML adding opening tags to a LIFO Queue, and removing (matching) opening tags from the start of the queue when a closing tag is encountered - performing queue shifting to allow for tag mismatches.

Finbarr
Is this answer in response to @Raha's question about writing one's own HTML parser?
Alan Moore
+1  A: 

I want to get keyword content from HTML tag I wrote:

Pattern keyLineContents = Pattern.compile("<(.[^<]*)(keywords)(.[^<]*)>");
Matcher keyLineMatcher = keyLineContents.matcher(documentURL);
boolean result = keyLineMatcher.find();
while(result)
{
  String metaTagContent = keyLineMatcher.group(1) + " " + keyLineMatcher.group(3);
  Pattern kcontent = Pattern.compile("(.*?content=\")(.[^<]*?)(\".[^<]*?)");
  Matcher keyLineMatcher2 = kcontent.matcher(metaTagContent);
  boolean result2 = keyLineMatcher.find();
  while (result2)
  {
    String metaTagContent2 = keyLineMatcher.group(1);
    result2 = keyLineMatcher.find();
  }
}

But I don't understand why my result2 is false. Result one is fine give all content of keyword tag

thanks

Elham
Try these regexes instead: `"<([^<]*)(keywords)([^<]*)>"` and `".*?content=\"([^<]*?)\""`
Alan Moore