【发布时间】:2015-04-10 03:48:06
【问题描述】:
我有一个 html 字符串,例如:
一个链接是sajfhds iufl
如何将此html字符串转换为包含链接但不包含任何html标签的字符串: 结果应该是:
一个链接是http://image.html
【问题讨论】:
标签: android html parsing hyperlink href
我有一个 html 字符串,例如:
一个链接是sajfhds iufl
如何将此html字符串转换为包含链接但不包含任何html标签的字符串: 结果应该是:
一个链接是http://image.html
【问题讨论】:
标签: android html parsing hyperlink href
String regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
String regex = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
String regex = "<\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]>"; // matches <http://google.com>
String regex = "<^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]>"; // does not match <http://google.com>
【讨论】:
你会得到类似这样的字符串
One link is <a href="http://image.html">sajfhds iufl</a>
你需要的是
One link is <a href="http://image.html">http://image.html</a>
所以,你应该做的是使用下面的代码找到模式
//imports required
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String stringToSearch = "<a href = \"http://image.html\" > sajfhds iufl</a>";
// the pattern we want to search for
Pattern p = Pattern.compile("<a href\\s*=\\s*\"(.+?)\"\\s*>(.+?)</a>");
Matcher m = p.matcher(stringToSearch);
if (m.find())
{
String temp = stringToSearch.replace(m.group(2), m.group(1));
//use the temp string for display
}
【讨论】: