'How do I replace custom BBCode style tag with a hyperlink
I've got a bunch of text which can contain custom tags in this format:
[MYLINK ID="1234" URL="http://mywebsite.com" TEXT="Website link"]
The text can contain multiple links. What I'm trying to do is to translate the tags to normal html links and grab the IDs in C# code. The resulting replaced tag should be in this format:
<a href="http://mywebsite.com?id=1234">Website link</a>
So to clarify, if I had the following chunk of text:
This is a test [MYLINK ID="1234" URL="http://mywebsite.com" TEXT="website link"] with some more text and [MYLINK ID="2345" URL="http://mywebsite2.com" TEXT="another link"] here too.
It should translate to this:
This is a test <a href="http://mywebsite.com?id=1234">website link</a> with some more text and <a href="http://mywebsite2.com?id=2345">another link</a> here too.
EDIT: Been faffing with Regex for the last couple of hours and have managed to get the following to match the tag but don't know what to do next...
\[MYLINK ID=\"(.*?)\" URL=\"(.*?)\" TEXT=\"(.*?)\"\]
Any help would be greatly appreciated. Cheers,
Solution 1:[1]
Thanks - I literally just got it as you were posting, doing something very similar...
string pattern = @"\[MYLINK ID=\""(.*?)\"" URL=\""(.*?)\"" TEXT=\""(.*?)\""\]";
text = Regex.Replace(text, pattern, x => $"<a href='{x.Groups[2].Value}?id={x.Groups[1].Value}'>{x.Groups[3].Value}</a>");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Simon |
