如何替换 java 或 c# 中的@mention
How can I replace @mention in java or c#
我想用 link
替换包含很多提及的字符串
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>
顺便说一下,我不想使用 for 或 foreach...
非常感谢
这是一个 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/\">[=10=]</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>
我想用 link
替换包含很多提及的字符串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>
顺便说一下,我不想使用 for 或 foreach...
非常感谢
这是一个 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/\">[=10=]</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>