2010-09-19 68 views
0

我有一個文本文件,它是將HTML解析爲純文本文件的結果。我需要擺脫的是類似像下面顯示的是XHTML評論:獲取<! - 和// - >之間的內容

<!-- 
if (!document.phpAds_used) 
document.phpAds_used = ','; 
phpAds_random = new String 
(Math.random()); phpAds_random = 
phpAds_random.substring(2,11); 
document.write ("<" + "script 
language='JavaScript' 
type='text/javascript' src='"); 
document.write 
("http://www.writers.net/Openads/adjs.php?n=" 
+ phpAds_random); document.write ("&what=zone:5&target=_blank"); 
document.write ("&exclude=" + 
document.phpAds_used); if 
(document.referrer) document.write 
("&referer=" + 
escape(document.referrer)); 
document.write ("'><" + "/script>"); 
// --> 

我怎樣才能擺脫使用Java <!--//-->之間什麼?

+0

該文件是否包含其他HTML?如果是這樣,你想保留它還是擺脫(一些)它? – BalusC 2010-09-19 23:25:38

回答

1

一個簡單的解決方案是使用String.replaceAll()方法。

例如,類似下面的代碼應工作:

String x = "wow <!-- // --> zip, here's <!-- comment here //--> another one"; 
x = x.replaceAll("<!--.*?//\\s*-->", ""); 
System.out.println(x); // prints out "wow zip, here's another one" 

,因爲你的榜樣的\\s*比賽沒有或許多空間有空間,但你的描述沒有。該.*?使這是一個非貪婪匹配,因此將匹配到第一個//-->

如果你是這一遍又一遍運行時,您可以使用Pattern代替,只是重新爲你處理每個塊的匹配:

Pattern.compile("<!--.*?//\\s*-->").matcher(x).replaceAll("")