看板java
标 题Re: [问题] 如何取得html标签中的值??
发信站无名小站 (Tue Oct 10 09:15:57 2006)
转信站ptt!ctu-reader!ctu-peer!news.nctu!netnews.csie.nctu!wretch
Pattern pattern = Pattern.compile("href=\"[^\"]*");
Matcher m = pattern.matcher(sb.toString());
while(m.find()) {
String str = m.group();
str = str.replaceFirst("href=\"", "");
str = str.substring(0, str.length()-1);
System.out.println(str);
}
Instead of regular expression,
HTML parser might be the more appropriate and pretty solution.
If you are interested, read this site
http://htmlparser.sourceforge.net/
※ 引述《[email protected] (空白之才)》之铭言:
> import java.util.regex.*;
> import java.io.*;
> public class Test1 {
> public static void main(String[] args) throws Exception {
> StringBuffer sb = new StringBuffer();
> BufferedReader br = new BufferedReader(new FileReader("c:/work/112.txt"));
> String line="";
> while( (line=br.readLine()) != null) {
> sb.append(line);
> }
> Pattern pattern = Pattern.compile(" "); //里面要怎麽用才能把我要的网址抽出来?
> Matcher m = pattern.matcher(sb.toString());
> while(m.find()) {
> String str = m.group();
> System.out.println(str);
> }
> }
> }
> 假设在infile.txt里面有内容如下
> <a class=l href="http://www.ntnu.edu.tw/art/" onmousedown="
> <a class=l href="http://203.71.53.40/" onmousedown="
> <a class=l href="http://www.scdxart.com/" onmousedown="return
> 我只想要里面的网址如下
> http://www.ntnu.edu.tw/art/
> http://203.71.53.40/
> http://www.scdxart.com/
> Pattern pattern = Pattern.compile(" ");
> 这行里面要怎麽用
> 我现在只弄到这样 但还是不对
> Pattern pattern = Pattern.compile("http://[^\"\\s]+");
--
夫兵者不祥之器物或恶之故有道者不处君子居则贵左用兵则贵右兵者不祥之器非君子
之器不得已而用之恬淡为上胜而不美而美之者是乐杀人夫乐杀人者则不可得志於天下
矣吉事尚左凶事尚右偏将军居左上将军居右言以丧礼处之杀人之众以哀悲泣之战胜以
丧礼处之道常无名朴虽小天下莫能臣侯王若能守之万物将自宾天地相合以降甘露民莫
之令而自均始制有名名亦既有夫亦将知止知止可以不殆譬道之在天下 60.50.17.74海