如何在java或c#中替换@mention

时间:2016-05-20 19:45:03

标签: java c# android replace mention

我想用链接替换包含许多提及的字符串

string comment = "@David you are best friend of @fri.tara3 and best of @mahta_";

string  pattern = "@[a-zA-Z0-9_.]+?(?![a-zA-Z0-9_.])";

我想要的是这样的:

<a href="http://Domain.com/David">@David</a> you are best friend of <a href="http://Domain.com/fri.tara3_">@fri.tara3_</a>

顺便说一句,我不想​​使用或预约...

非常感谢

1 个答案:

答案 0 :(得分:0)

这是一个Java正则表达式解决方案:

String str = "@David you are best friend of @fri.tara3 and best of @mahta_";
String formatted = str.replaceAll("@([^\\s]+)", "<a href=\"http://Domain.com/$1\">$0</a>");

输出:

<a href="http://Domain.com/David">@David</a> you are best friend of <a href="http://Domain.com/fri.tara3">@fri.tara3</a> and best of <a href="http://Domain.com/mahta_">@mahta_</a>