how html tags are extracted using java?

I would like to need the java code for extracting html tags.

View Answers

August 28, 2012 at 4:27 PM

Here is a java example that extract a tag from a line of HTML using the Pattern and Matcher classes.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExtractContentFromHTMLTag
{
  public static void main(String[] args)
  {
    String stringToSearch = "<p>Hello <h1>Welcome To Roseindia</h1> ...</p>";

    Pattern p = Pattern.compile("<h1>(\\S+)</h1>");
    Matcher m = p.matcher(stringToSearch);

    if (m.find()){
      String codeGroup = m.group(1);
      System.out.format("'%s'\n", codeGroup);
    }
  }
}

August 28, 2012 at 4:33 PM

Modify the above code.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExtractContentFromHTMLTag
{
  public static void main(String[] args)
  {
    String stringToSearch = "<p>Hello <h1>WelcomeToRoseindia</h1> ..</p>";

    Pattern p = Pattern.compile("<h1>(\\S+)</h1>");
    Matcher m = p.matcher(stringToSearch);

    if (m.find())
    {
      String codeGroup = m.group(1);

      System.out.format("'%s'\n", codeGroup);
    }

  }
}









Related Tutorials/Questions & Answers:
Advertisements