2013-10-30 180 views
3

是否有表達式可以獲取兩個HTML標籤之間的值?此外,如果跨度標籤是有那麼我需要保持,因爲它是正則表達式刪除一些HTML標籤,但保留跨度標籤

input 
<table><tr> 
<td>abc<td/> <span class="abc">Test</span> 
</tr> 
</table> 

Output 

abc <span class"abc"> Test</span> 

我嘗試以下解決方案,但它去掉也

String input="<table><tr><td>abc<td/> <span>Test</span></tr></table>"; 
     String newValue = input.replaceAll("<[^>]*>", ""); 
     System.out.println(newValue); 

輸出上面的代碼

abc Test 

但輸出標籤要求

abc <span class"abc"> Test</span> 
+0

你有一些代碼? –

+0

我嘗試刪除Html標記like-- input.replaceAll(「<[^>」*>「,」「);但我需要保持標籤原樣。以上代碼刪除所有html代碼 – Raje

+0

@Raje看到,您需要在您的問題中提供該代碼。否則,我們會認爲你還沒有嘗試過任何東西:) – HamZa

回答

2

您可以使用負面預測(?!...),這意味着不會跟着來測試標記。爲例在Java語法:

<(?!/?span\\b)[^>]*> 
1

我認爲這會做你在找什麼:

str.replaceAll("<(?!\\/?span)[^>]+>", "") 

這將尋找一個<,然後向前看,看看它是否包含/spanspan來臨前直到下一個> ......並全部替換爲無。

Example

String str = "<table><tr><td>abc<td/> <span class=\"abc\">Test</span></tr></table>\";"; 
System.out.println(str.replaceAll("<(?!\\/?span)[^>]+>", "")); 
//prints: abc <span class="abc">Test</span>";