Java Regular Expression parsing HTML sample sharing _java

Source: Internet
Author: User

Copy Code code as follows:

Package work;

Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;

Import Org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
Import org.apache.commons.httpclient.HttpClient;
Import org.apache.commons.httpclient.HttpException;
Import Org.apache.commons.httpclient.HttpStatus;
Import Org.apache.commons.httpclient.methods.GetMethod;
Import Org.apache.commons.httpclient.params.HttpMethodParams;

public class Chuanboyi {

public static void Main (string[] args) {
TODO auto-generated Method Stub
StringBuffer html = new StringBuffer ();
HttpClient httpclient = new HttpClient ();
Create a Get Method instance
GetMethod GetMethod = new GetMethod ("http://www.jb51.net");
Use the system-provided default recovery policy
Getmethod.getparams (). Setparameter (Httpmethodparams.retry_handler, New Defaulthttpmethodretryhandler ());
try{
Execute Get method
int statusCode = Httpclient.executemethod (GetMethod);
if (StatusCode!= httpstatus.sc_ok) {
System.out.println ("method is wrong" + getmethod.getstatusline ());
}
InputStream responsebody = Getmethod.getresponsebodyasstream ();
BufferedReader reader = new BufferedReader (new InputStreamReader (Responsebody, "utf-8"));
String line = Reader.readline ();
while (line!= null) {
Html.append (line). append ("\ n");
line = Reader.readline ();
}
Reader.close ();
Regular expressions
String regex = "<form name=\" compareform\ "[\\s\\s]+>[\\s\\s]+</form>.*<script.*>";
String regexa = "(?<=<li>) [\\s\\s]+? (?=</li>) ";
Pattern pattern = pattern.compile (regex);
Matcher m = pattern.matcher (HTML);
StringBuffer str = new StringBuffer ();
int i = 0;
while (M.find ()) {
Str.append (M.group ());
}
Pattern = Pattern.compile (regexa);
m = Pattern.matcher (str);
while (M.find ()) {
Attrs (M.group ());
i++;
}
System.out.println ("A total of" +i+ "piece of data!) ");
}catch (HttpException e) {
Todo:handle exception
System.out.println ("Please check your provided HTTP address!");
E.printstacktrace ();
}catch (IOException e) {
Todo:handle exception
SYSTEM.OUT.PRINTLN ("The line is wrong!");
E.printstacktrace ();
}finally{
Getmethod.releaseconnection ()//Free link
}
}
public static void Attrs (String str) {

Gets the regular expression of the URL
String Regexurl = "[a-z]+-[0-9]+\\.html";
Gets the regular expression of name
String regexname = "(? <=title=\") [[\\w-\\s][^x00-xff]]+ (? =\ ")";
Get a regular expression for a picture
String regexpicture = "Images.*\\.jpg";

Pattern Patternurl = Pattern.compile (Regexurl);
Pattern Patternname = Pattern.compile (regexname);
Pattern patternpicture = Pattern.compile (regexpicture);
Matcher Murl = Patternurl.matcher (str);
Matcher mname = Patternname.matcher (str);
Matcher mpicture = Patternpicture.matcher (str);
if (Mname.find ()) {
System.out.println ("Name:" +mname.group ());
}
if (Murl.find ()) {
System.out.println ("Link:" +murl.group ());
}
if (Mpicture.find ()) {
System.out.println ("Picture:" +mpicture.group ());
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.