Re: [問題] 用Regex去消除html的hyperlink

看板C_Sharp (C#)作者 (jiing)時間20年前 (2005/08/27 08:26), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《heliosy (這一欄是要做啥用的)》之銘言: : 以下是我試寫的簡單程式碼 : Regex re = new Regex("<a href=.*\">"); : StreamReader sr = new StreamReader("a.htm"); : string line = null; : string all = null; : while((line = sr.ReadLine()) != null) : { : if(line.IndexOf("<a href=") != -1) : { : all += re.Replace(line,"").Replace("</a>","") + "\r\n"; : } : else : { : all += line + "\r\n"; : } : } : sr.Close(); : StreamWriter sw = new StreamWriter("b.htm"); : sw.Write(all); : sw.Flush(); : sw.Close(); : 但是這樣如果遇到一行中有兩個hyperlink的話 : 就會把第一個hyperlink全都刪掉了 : 有什麼判斷方式可以讓他遇到第一個</a>就停止,然後再繼續判斷下一個嗎 : 謝謝 要用不貪多比對的? Perl 語法概念上是 $str=~ /<a href(.*?)<\/a>/g; ^^ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.120.12.63
文章代碼(AID): #133xCV_U (C_Sharp)
文章代碼(AID): #133xCV_U (C_Sharp)