2016-05-20 5 views
0

Ich möchte eine Zeichenfolge ersetzen, enthält viele Erwähnung mit einem LinkWie ich @mention in Java oder 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_.])"; 

Was ich möchte so etwas wie dieses ersetzen:

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

Durch die Art und Weise, ich will nicht für oder foreach verwenden ...

Vielen Dank

+0

Bitte wählen Sie eine Sprache und uns nur zeigen, was Sie – Reimeus

+0

dem es versucht haben, Java oder C#? Was hast du bisher versucht? –

+0

ist es nicht wichtig, ich will nur lernen – ara

Antwort

0

Hier ist eine 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>"); 

Ausgang:

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