2013-01-15 47 views
-4

我需要一個正則表達式來匹配文本文件和html文件。忽略html標籤匹配段落從文本文件到html文件

"<b>Dog</b> and Cat" 

這將等於

"Dog and Cat" 

我使用Java。

+0

用'「」替換'<.*?>'並比較。 – sp00m

+0

[剝離HTML中的HTML標記]可能的重複(http://stackoverflow.com/questions/832620/stripping-html-tags-in-java) – rds

回答

0

以下是您需要的代碼段。

String text, html; 
BufferedReader br; 
int i;int tags=0; 

br=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("Enter HTML code");//If you want to read ferom file, replace the code 
html=br.readLine(); 
for(i=0;i<html.length();i++){ 
if(html.charAt(i)=='<'){tags++;continue;} 
if(html.charAt(i)=='>'){tags--;continue;} 
if(tags==0)text=text+html.charAt(i); 
} 
+0

如何從html中獲取該文本的起始索引,但忽略標記同時比較?該函數的返回值必須是索引號。我試圖在該位置插入一些字符。 – mois

+0

感謝您的第一個答案..它幫助我 – mois

+0

@mois如果它有幫助,然後選擇我的答案左側的剔號int –