我需要一个 Java 代码来获取此文本“ text1 [example1]http://example1.com [example2]http://example2.com text2 ....”并将其返回如下:
text1 <a target='_blank' href='http://example1.com'>example1</a> <a target='_blank' href='http://example2.com'>example2</a> text2
我的意思是每当它在文本中找到此模式 [example2]http://example2.com 并将其放入 html 超链接中。请帮助..我到目前为止所做的但它不起作用
ViolationTableItem item = new ViolationTableItem(m_violation);
String dataSource = "";
String copyDataSource="";
try {
dataSource = item.getUi().getViolation().getBlackListEntries()
.getDataSources();
Matcher matcher = Pattern.compile(
"(\\[.*?\\])(.*://[^<>[:space:]]+[[:alnum:]/])").matcher(
dataSource);
while (matcher.find()) {
String matchedLink = matcher.group();
Matcher nameMatcher = Pattern.compile("\\[.*?\\]").matcher(
matchedLink);
String nameMatched = "";
String nameMatched2 = "";
String linkableText = "";
String[] tst = matchedLink.split("\\[.*?\\]");
for (int i = 1; i < tst.length; i++) {
if (nameMatcher.find()) {
nameMatched = nameMatcher.group();
// System.out.println(nameMatched);
nameMatched2 = matchedLink.replace(nameMatched, "");
// System.out.println(nameMatched2);
}
if (tst[i] != null && !tst[i].equals("")) {
linkableText = "<a target='_blank' href='"
+ tst[i]
+ "'>"
+ nameMatched.replaceAll("\\[", "").replaceAll(
"\\]", "") + "</a>";
copyDataSource += dataSource.replace(matchedLink,
linkableText) + " ";
}
}
}
} catch (Exception ne){
return copyDataSource;
}
return copyDataSource;
提前致谢!