2017-10-06 57 views
-1

我從Excel中獲取數據,並獲取細胞的值與文本替換裏面的html 標籤如何使用Java

"abc def (<a href = "https://www.example.com">terms and conditions apply</a>)"

,我想將其轉換成

"abc def (terms and conditions apply)" 

任何想法如何做到這一點?

+0

你的主題說href,消息說def?是錯字嗎?什麼是實際的字符串? – Optional

+0

搜索左括號和大於符號的結尾'>'並擦除它們之間的內容。只有這些值是一致的,這纔會起作用。 –

+0

或者你可以使用解析器和刪除屬性的href和使用元素名稱和值來創建任何動態字符串的字符串 – Optional

回答

0
String s = "abc def (<a href = \"https://www.example.com\">terms and conditions apply</a>)"; 
    String a = s.replaceAll("<a(.*?)>|</a>", ""); 
+0

但是我的情況是我正在讀取excel中的單元格值。它的值是「abc def()」。但我想「abc def(條款和條件適用)」 – Arvinder

+0

我已編輯答案。 – Sugan

0
String s= "abc def (https://www.example.com\">terms and conditions apply)"; 
s = s.substring(0, s.indexOf('(')+1)+s.substring(s.indexOf('>')+1); 

這是你在找什麼?

+0

謝謝..但是我的情況是我正在讀excel中的單元格值。它的值是「abc def(terms and conditions apply)」。 但我想「abc def(條款和條件適用)」。 – Arvinder

0

試試這個

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class Main{ 
    public static void main(String[] args) { 
     String regex = "<a(.*)>(.*)<\\/a>"; 
     String string = "abc def (<a href = \"example.com\">terms and conditions apply</a>)"; 
     string = string.replaceAll(regex, "$2"); 
     System.out.println(string); 
    } 
} 

輸出

abc def (terms and conditions apply) 

和輸入

abc def (<a href="/some/some/some/href"># this is another sample #</a>) ghi

輸出

abc def (# this is another sample #) ghi

+0

如果之間有多行(帶有\ n),則可能需要修改。 – Sugan