---
title: "java读取（正则表达式分析）网页内容"
author: "cjc"
date: "2008-01-22T10:20:00Z"
tags: []
slug: "java"
draft: false
lastmod: "2008-01-22T10:20:00Z"
id: 298688
url: "https://cjc.iteye.com/blog/298688"
---

由于原来的“插入代码”复制方法要把转义符过滤掉，所以下面用文本粘贴：

package com.xiaofeng.picup;

import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.net.MalformedURLException;  
import java.net.URL;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;

/\**  
 *  
 * @author 晓峰2007.1.18 抓取雅虎知识堂的文章标题及内容（测试） 手动输入网址抓取，可进一步自动抓取整个知识堂的全部内容  
 *  
 \*/  
public class WebContent  
{  
 /\**  
  * 读取一个网页全部内容  
  \*/  
 public String getOneHtml(final String htmlurl) throws IOException  
 {  
  URL url;  
  String temp;  
  final StringBuffer sb = new StringBuffer();  
  try  
  {  
   url = new URL(htmlurl);  
   final BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));// 读取网页全部内容  
   while ((temp = in.readLine()) != null)  
   {  
    sb.append(temp);  
   }  
   in.close();  
  }  
  catch (final MalformedURLException me)  
  {  
   System.out.println("你输入的URL格式有问题！请仔细输入");  
   me.getMessage();  
   throw me;  
  }  
  catch (final IOException e)  
  {  
   e.printStackTrace();  
   throw e;  
  }  
  return sb.toString();  
 }

 /\**  
  *  
  * @param s  
  * @return 获得网页标题  
  \*/  
 public String getTitle(final String s)  
 {  
  String regex;  
  String title = "";  
  final List&lt;String&gt; list = new ArrayList&lt;String&gt;();  
  regex = "&lt;title&gt;.\*?&lt;/title&gt;";  
  final Pattern pa = Pattern.compile(regex, Pattern.CANON\_EQ);  
  final Matcher ma = pa.matcher(s);  
  while (ma.find())  
  {  
   list.add(ma.group());  
  }  
  for (int i = 0; i &lt; list.size(); i++)  
  {  
   title = title + list.get(i);  
  }  
  return outTag(title);  
 }

 /\**  
  *  
  * @param s  
  * @return 获得链接  
  \*/  
 public List&lt;String&gt; getLink(final String s)  
 {  
  String regex;  
  final List&lt;String&gt; list = new ArrayList&lt;String&gt;();  
  regex = "&lt;a\[^&gt;]\*href=(\\"(\[^\\"]\*)\\"|\\'(\[^\\']\*)\\'|(\[^\\\\s&gt;]\*))\[^&gt;]\*&gt;(.\*?)&lt;/a&gt;";  
  final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);  
  final Matcher ma = pa.matcher(s);  
  while (ma.find())  
  {  
   list.add(ma.group());  
  }  
  return list;  
 }

 /\**  
  *  
  * @param s  
  * @return 获得脚本代码  
  \*/  
 public List&lt;String&gt; getScript(final String s)  
 {  
  String regex;  
  final List&lt;String&gt; list = new ArrayList&lt;String&gt;();  
  regex = "&lt;script.\*?&lt;/script&gt;";  
  final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);  
  final Matcher ma = pa.matcher(s);  
  while (ma.find())  
  {  
   list.add(ma.group());  
  }  
  return list;  
 }

 /\**  
  *  
  * @param s  
  * @return 获得CSS  
  \*/  
 public List&lt;String&gt; getCSS(final String s)  
 {  
  String regex;  
  final List&lt;String&gt; list = new ArrayList&lt;String&gt;();  
  regex = "&lt;style.\*?&lt;/style&gt;";  
  final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);  
  final Matcher ma = pa.matcher(s);  
  while (ma.find())  
  {  
   list.add(ma.group());  
  }  
  return list;  
 }

 /\**  
  *  
  * @param s  
  * @return 去掉标记  
  \*/  
 public String outTag(final String s)  
 {  
  return s.replaceAll("&lt;.\*?&gt;", "");  
 }

 /\**  
  *  
  * @param s  
  * @return 获取雅虎知识堂文章标题及内容  
  \*/  
 public HashMap&lt;String, String&gt; getFromYahoo(final String s)  
 {  
  final HashMap&lt;String, String&gt; hm = new HashMap&lt;String, String&gt;();  
  final StringBuffer sb = new StringBuffer();  
  String html = "";  
  System.out.println("\\n------------------开始读取网页(" + s + ")--------------------");  
  try  
  {  
   html = getOneHtml(s);  
  }  
  catch (final Exception e)  
  {  
   e.getMessage();  
  }  
  // System.out.println(html);  
  System.out.println("------------------读取网页(" + s + ")结束--------------------\\n");  
  System.out.println("------------------分析(" + s + ")结果如下--------------------\\n");  
  String title = outTag(getTitle(html));  
  title = title.replaceAll("\_雅虎知识堂", "");  
  // Pattern pa=Pattern.compile("&lt;div  
  // class=\\"original\\"&gt;(.\*?)((\\r\\n)\*)(.\*?)((\\r\\n)\*)(.\*?)&lt;/div&gt;",Pattern.DOTALL);  
  final Pattern pa = Pattern.compile("&lt;div class=\\"original\\"&gt;(.\*?)&lt;/p&gt;&lt;/div&gt;", Pattern.DOTALL);  
  final Matcher ma = pa.matcher(html);  
  while (ma.find())  
  {  
   sb.append(ma.group());  
  }  
  String temp = sb.toString();  
  temp = temp.replaceAll("(&lt;br&gt;)+?", "\\n");// 转化换行  
  temp = temp.replaceAll("&lt;p&gt;&lt;em&gt;.\*?&lt;/em&gt;&lt;/p&gt;", "");// 去图片注释  
  hm.put("title", title);  
  hm.put("original", outTag(temp));  
  return hm;

 }

 /\**  
  *  
  * @param args  
  *            测试一组网页，针对雅虎知识堂  
  \*/  
 public static void main(final String args\[])  
 {  
  String url = "";  
  final List&lt;String&gt; list = new ArrayList&lt;String&gt;();  
  System.out.print("输入URL，一行一个，输入结束后输入 go 程序开始运行:   \\n");  
  /*  
   * [http://ks.cn.yahoo.com/question/1307121201133.html](http://ks.cn.yahoo.com/question/1307121201133.html)  
   * [http://ks.cn.yahoo.com/question/1307121101907.html](http://ks.cn.yahoo.com/question/1307121101907.html)  
   * [http://ks.cn.yahoo.com/question/1307121101907\_2.html](http://ks.cn.yahoo.com/question/1307121101907_2.html)  
   * [http://ks.cn.yahoo.com/question/1307121101907\_3.html](http://ks.cn.yahoo.com/question/1307121101907_3.html)  
   * [http://ks.cn.yahoo.com/question/1307121101907\_4.html](http://ks.cn.yahoo.com/question/1307121101907_4.html)  
   * [http://ks.cn.yahoo.com/question/1307121101907\_5.html](http://ks.cn.yahoo.com/question/1307121101907_5.html)  
   * [http://ks.cn.yahoo.com/question/1307121101907\_6.html](http://ks.cn.yahoo.com/question/1307121101907_6.html)  
   * [http://ks.cn.yahoo.com/question/1307121101907\_7.html](http://ks.cn.yahoo.com/question/1307121101907_7.html)  
   * [http://ks.cn.yahoo.com/question/1307121101907\_8.html](http://ks.cn.yahoo.com/question/1307121101907_8.html)  
   \*/  
  final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  try  
  {  
   while (!(url = br.readLine()).equals("go"))  
   {  
    list.add(url);  
   }  
  }  
  catch (final Exception e)  
  {  
   e.getMessage();  
  }  
  final WebContent wc = new WebContent();  
  HashMap&lt;String, String&gt; hm = new HashMap&lt;String, String&gt;();  
  for (int i = 0; i &lt; list.size(); i++)  
  {  
   hm = wc.getFromYahoo(list.get(i));  
   System.out.println("标题： " + hm.get("title"));  
   System.out.println("内容： \\n" + hm.get("original"));  
  }  
  /*  
   * String htmlurl\[] = {  
   * "[http://ks.cn.yahoo.com/question/1307121201133.html](http://ks.cn.yahoo.com/question/1307121201133.html)",  
   * "[http://ks.cn.yahoo.com/question/1307121101907.html](http://ks.cn.yahoo.com/question/1307121101907.html)",  
   * "[http://ks.cn.yahoo.com/question/1307121101907\_2.html](http://ks.cn.yahoo.com/question/1307121101907_2.html)",  
   * "[http://ks.cn.yahoo.com/question/1307121101907\_3.html](http://ks.cn.yahoo.com/question/1307121101907_3.html)",  
   * "[http://ks.cn.yahoo.com/question/1307121101907\_4.html](http://ks.cn.yahoo.com/question/1307121101907_4.html)",  
   * "[http://ks.cn.yahoo.com/question/1307121101907\_5.html](http://ks.cn.yahoo.com/question/1307121101907_5.html)",  
   * "[http://ks.cn.yahoo.com/question/1307121101907\_6.html](http://ks.cn.yahoo.com/question/1307121101907_6.html)",  
   * "[http://ks.cn.yahoo.com/question/1307121101907\_7.html](http://ks.cn.yahoo.com/question/1307121101907_7.html)",  
   * "[http://ks.cn.yahoo.com/question/1307121101907\_8.html](http://ks.cn.yahoo.com/question/1307121101907_8.html)" }; WebContent  
   * wc = new WebContent(); HashMap&lt;String, String&gt; hm = new HashMap&lt;String,  
   * String&gt;(); for (int i = 0; i &lt; htmlurl.length; i++) { hm =  
   * wc.getFromYahoo(htmlurl\[i]); System.out.println("标题： " +  
   * hm.get("title")); System.out.println("内容： \\n" + hm.get("original")); }  
   \*/  
  /*  
   * String html=""; String link=""; String sscript=""; String content="";  
   * System.out.println(htmlurl+" 开始读取网页内容：");  
   * html=wc.getOneHtml(htmlurl); System.out.println(htmlurl+"  
   * 读取完毕开始分析……"); html=html.replaceAll("(&lt;script.\*?)((\\r\\n)\*)(.\*?)((\\r\\n)\*)(.\*?)(&lt;/script&gt;)","  
   * ");//去除脚本 html=html.replaceAll("(&lt;style.\*?)((\\r\\n)\*)(.\*?)((\\r\\n)\*)(.\*?)(&lt;/style&gt;)","  
   * ");//去掉CSS html=html.replaceAll("&lt;title&gt;.\*?&lt;/title&gt;"," ");//除去页面标题  
   * html=html.replaceAll("&lt;a\[^&gt;]\*href=(\\"(\[^\\"]\*)\\"|\\'(\[^\\']\*)\\'|(\[^\\\\s&gt;]\*))\[^&gt;]\*&gt;(.\*?)&lt;/a&gt;","  
   * ");//去掉链接 html=html.replaceAll("([\\\\s){2](),}?"," ");//除去多余空格  
   * html=wc.outTag(html);//多余标记 System.out.println(html);  
   \*/

  /*  
   * String s\[]=html.split(" +"); for(int i=0;i&lt;s.length;i++){  
   * content=(content.length()&gt;s\[i].length())?content:s\[i]; }  
   * System.out.println(content);  
   \*/

  // System.out.println(htmlurl+"网页内容结束");  
  /*  
   * System.out.println(htmlurl+"网页脚本开始："); List  
   * script=wc.getScript(html); for(int i=0;i&lt;script.size();i++){  
   * System.out.println(script.get(i)); }  
   * System.out.println(htmlurl+"网页脚本结束：");  
   *  
   * System.out.println(htmlurl+"CSS开始："); List css=wc.getCSS(html);  
   * for(int i=0;i&lt;css.size();i++){ System.out.println(css.get(i)); }  
   * System.out.println(htmlurl+"CSS结束：");  
   *  
   * System.out.println(htmlurl+"全部链接内容开始："); List list=wc.getLink(html);  
   * for(int i=0;i&lt;list.size();i++){ link=list.get(i).toString(); }  
   * System.out.println(htmlurl+"全部链接内容结束：");  
   *  
   * System.out.println("内容"); System.out.println(wc.outTag(html));  
   \*/  
 }  
}

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package com.xiaofeng.picup;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.BufferedReader;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.IOException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.InputStreamReader;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.net.MalformedURLException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.net.URL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.ArrayList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.HashMap;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.List;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.regex.Matcher;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.regex.Pattern;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) *   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @author 晓峰2007.1.18 抓取雅虎知识堂的文章标题及内容（测试） 手动输入网址抓取，可进一步自动抓取整个知识堂的全部内容  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) *   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class WebContent ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * 读取一个网页全部内容  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String getOneHtml(String htmlurl) throws IOException...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        URL url;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String temp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        StringBuffer sb = new StringBuffer();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        try ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            url = new URL(htmlurl);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            BufferedReader in = new BufferedReader(new InputStreamReader(url  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    .openStream(), "utf-8"));// 读取网页全部内容  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            while ((temp = in.readLine()) != null) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                sb.append(temp);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            in.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        }catch(MalformedURLException me)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            System.out.println("你输入的URL格式有问题！请仔细输入");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            me.getMessage();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            throw me;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        }catch (IOException e) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            throw e;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return sb.toString();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param s  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return 获得网页标题  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String getTitle(String s) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String regex;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String title = "";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        List&lt;String&gt; list = new ArrayList&lt;String&gt;();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        regex = "&lt;title&gt;.\*?&lt;/title&gt;";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Pattern pa = Pattern.compile(regex, Pattern.CANON\_EQ);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Matcher ma = pa.matcher(s);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        while (ma.find()) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            list.add(ma.group());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        for (int i = 0; i &lt; list.size(); i++) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            title = title + list.get(i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return outTag(title);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param s  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return 获得链接  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public List&lt;String&gt; getLink(String s) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String regex;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        List&lt;String&gt; list = new ArrayList&lt;String&gt;();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        regex = "&lt;a\[^&gt;]\*href=("(\[^"]\*)"|'(\[^']\*)'|(\[^\\s&gt;]\*))\[^&gt;]\*&gt;(.\*?)&lt;/a&gt;";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Pattern pa = Pattern.compile(regex, Pattern.DOTALL);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Matcher ma = pa.matcher(s);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        while (ma.find()) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            list.add(ma.group());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return list;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param s  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return 获得脚本代码  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public List&lt;String&gt; getScript(String s) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String regex;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        List&lt;String&gt; list = new ArrayList&lt;String&gt;();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        regex = "&lt;script.\*?&lt;/script&gt;";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Pattern pa = Pattern.compile(regex, Pattern.DOTALL);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Matcher ma = pa.matcher(s);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        while (ma.find()) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            list.add(ma.group());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return list;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param s  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return 获得CSS  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public List&lt;String&gt; getCSS(String s) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String regex;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        List&lt;String&gt; list = new ArrayList&lt;String&gt;();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        regex = "&lt;style.\*?&lt;/style&gt;";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Pattern pa = Pattern.compile(regex, Pattern.DOTALL);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Matcher ma = pa.matcher(s);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        while (ma.find())