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,
CodePudding user response:
If the attributes always in the order you showed, you can use
var text = "[MYLINK ID=\"1234\" URL=\"http://mywebsite.com\" TEXT=\"Website link\"]";
var pattern = "\\[MYLINK\\s ID=\"([^\"]*)\"\\s URL=\"([^\"]*)\"\\s TEXT=\"([^\"]*)\"]";
var replacement = "<a href=\"$2?id=$1\">$3</a>";
var result = Regex.Replace(text, pattern, replacement, RegexOptions.IgnoreCase);
// => <a href="http://mywebsite.com?id=1234">Website link</a>
See the $3" rel="nofollow noreferrer">.NET regex demo and the C# demo.
Details:
\[MYLINK
-[MYLINK
text\s
- any one or more whitespacesID=\"
-ID="
text([^\"]*)
- Group 1 ($1
): zero or more chars other than"
\"\s URL=\"
-"
, one or more whitespaces,URL="
text([^\"]*)
- Group 2 ($2
): zero or more chars other than"
\"\s TEXT=\"
-"
, one or more whitespaces,TEXT="
text([^\"]*)
- Group 3 ($3
): zero or more chars other than"
\"]
-"]
text.
CodePudding user response:
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>");