Re: [J2SE] html的文字能當成string用嗎?
※ 引述《brianpcbest (FF4)》之銘言:
: http://udn.com/NEWS/WORLD/WOR1/3804335.shtml
: 以上是我要截取的網頁
: 我在前面先宣告
: String endString = "聯合報";
: 截取時
: while ((lineStr=in.readLine())!= null )
: { if(lineStr.equals(endString))
: break;
: testStr=testStr.replaceAll("<[^>]*>","");
: testStr=testStr.replaceAll(" ","");
: }
: 原本的用意是我是只想要截取新聞~避免掉其他的html碼,
: 讓他讀到新聞結尾的聯合報就跳出
: 但是這樣的做法好像行不通
: 有人可以指點一下嗎?謝謝
1.裡面沒有任何一行的字串 達成你的Replace條件後 是"聯合報"
你應該要用 indexOf() 或者 matches() 做"包含有"的判斷。
2.另外聯合報在開頭和結尾各有一個不適合放在結尾
建議你可以取這行
<p class="story"><span id="source_name" class="story">【2007/04/15 聯合報】
</span><span class="author"><A href="http://udn.com/" target="_blank">@
</A><a href="http://udn.com/" target="_blank">http://udn.com/</a></span> <a
href="http://udn.com/?" target="_blank"><img
src="/1024/images/logo_small.gif" width="19" height="19" border="0"
align="absmiddle"></a></p>
replaceAll後變成 取
matches(".*【20[0-9]{2}/[0-1][0-9]/[0-3][0-9] 聯合報】")
當脫離點也是種選擇
可參考 String#matches
http://0rz.tw/072Ba
--
I am a person, and I am always thinking .
Thinking in love , Thinking in life ,
Thinking in why , Thinking in worth.
I can't believe any of what ,
I am just thinking then thinking ,
but worst of all , most of mine is thinking not actioning...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.134.27.68
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 5 篇):