【问题标题】:How can I replace @mention in java or c#如何在 java 或 c# 中替换 @mention
【发布时间】:2016-09-18 04:37:19
【问题描述】:

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

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还是C#?到目前为止,您尝试过什么?
  • 不重要,我只是想学习

标签: java c# android replace mention


【解决方案1】:

这是一个 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>

【讨论】:

    猜你喜欢
    • 2013-03-31
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    相关资源
    最近更新 更多