private static Pattern pattern = Pattern.compile("(http|https)://[\\w\\.\\-/:]+");
提取微博中的短链:
/**
* 获取微博中的短链
* @param content
* @return
*/
public static String[] getURL(String content) {
StringBuffer buffer = new StringBuffer();
String[] retStr;
if (content.indexOf("http:") != -1) {
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
buffer.append(matcher.group()).append(",");
}
if (buffer.length() > 0) {
buffer.setLength(buffer.length() - 1);
String temp = buffer.toString();
retStr = temp.split(",");
return retStr;
}
}
return null;
}
过滤微博中的短链:
/**
* 过滤短链接
* @param content
* @return
*/
public static String getFilterURL(String content) {
if (content.indexOf("http:") != -1) {
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
return matcher.replaceAll("");
}
}
return null;
}
过滤微博中@的用户名:
private static Pattern m = Pattern.compile("@.*? ");
或者
private static Pattern m = Pattern.compile("@\\S+?\\s");
public static String getFilterName(String content){
if (content.indexOf("@") != -1) {
Matcher matcher = m.matcher(content);
while (matcher.find()) {
return matcher.replaceAll("");
}
}
return content;
}