我想用Java中的<a href="link">text</a>
代替[text](link)
。我怎样才能做到这一点?如何在java中使用<a href="link">text</a>替换[文本](链接)?
在Objective-C是这样的:
NSRegularExpression *linkParsing = [NSRegularExpression regularExpressionWithPattern:@"(?<!\\!)\\[.*?\\]\\(\\S*\\)" options:NSRegularExpressionCaseInsensitive error:nil];
编辑
最后,基于svasa's方法,我不喜欢这样的:
public String parseText(String postText) {
Pattern p = Pattern.compile("\\[(.*)\\]\\((.*)\\)");
Matcher m = p.matcher(postText);
StringBuffer sb = new StringBuffer(postText.length());
while (m.find()) {
String found_text = m.group(1);
String found_link = m.group(2);
String replaceWith = "<a href=" + "\"" + found_link + "\"" + ">" + found_text + "</a>";
m.appendReplacement(sb, replaceWith);
}
return sb.toString();
}
这是更好,因为使用全部匹配文字。
@insidefun考虑投票的答案,这是SO的方式“谢谢你”:) – SmokeDispenser