Reputation: 585
I have the following string saved in one of my MSSQL tables:
"Inflicts [regular damage]{mechanic:regular-damage}."
I want to replace everything inside [] for a link pointing to mechanic:regular-damage, like this:
"Inflicts <a href=\"/Mechanics.aspx?Name=regular-damage\">regular damage</a>."
How could I do that? Thanks in advance.
Upvotes: 0
Views: 481
Reputation: 726559
You need to capture three things - the content of square brackets, the content of curly braces prior to the colon, and the rest of the content of curly braces. You can do it using this regex:
@"\[([^\]]+)\]{([^:]+):([^}]+)}"
// ^^^^^^^^ ^^^^^^^ ^^^^^^^
// Group$1 Group$2 Group$3
With these three groups captured, you can format the answer using this replacement template:
@"<a href=""$2.aspx?Name=$3"">$1</a>"
Putting it all together, you get
var s = @"Inflicts [regular damage]{mechanic:regular-damage}.";
var r = Regex.Replace(
s
, @"\[([^\]]+)\]{([^:]+):([^}]+)}"
, @"<a href=""$2.aspx?Name=$3"">$1</a>"
);
This produces
Inflicts <a href="mechanic.aspx?Name=regular-damage">regular damage</a>.
Upvotes: 2