2017-10-06 52 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