2016-05-20 1 views
0

Je veux remplacer une chaîne qui contient beaucoup de mention avec un lienComment puis-je remplacer @mention en java ou C#

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

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

Ce que je veux est quelque chose comme ceci:

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

Par façon, je ne veux pas utiliser ou foreach ...

Merci beaucoup

+0

S'il vous plaît choisir une seule langue et nous montrer ce que vous avez essayé – Reimeus

+0

qui est-il, Java ou C#? Qu'avez-vous essayé jusqu'à présent? –

+0

ce n'est pas important, je veux juste apprendre – ara

Répondre

0

Voici Java regex Solu tion:

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>"); 

Sortie:

<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>