1

Although I googled many searching, I didn't find a satisfactory result. I'm new to Regex, so I couldn't write anything. I have string like this: [Whatever You Want (WYW)]. And I want to get a result like this: [Whatever You Want (<b>WYW</b>)]. But strings change every condition like [New String (NS)] or [Other string (OTS)] etc.

I am aware that similar questions are asked here. But I couldn't reach a solution, I had to write it here.

I think, I have to use Regex but I have no idea. How can I do it?

Habip Oğuz
  • 659
  • 3
  • 14
  • Please check the linked question, try something, and if you fail please come back to share your findings and precise the problem. – Wiktor Stribiżew Jun 20 '19 at 15:31
  • In its very simplest, sub-optimal form, `var newString = oldString.Replace(findThis, $"{findThis}");` The fact that it's HTML probably isn't relevant. You're just finding a string and replacing it with a new string (which happens to contain the original string.) Regex *might* be good solution, but for now what you're doing looks like a really simple find-and-replace. For performance, perhaps use [`StringBuiilder.Replace()`](https://stackoverflow.com/questions/6524528/string-replace-vs-stringbuilder-replace). – Scott Hannen Jun 20 '19 at 15:37
  • @WiktorStribizew, I did not see any expression that written in c#. – Habip Oğuz Jun 20 '19 at 15:38
  • @ScottHannen, Thank for your comment. I edited my question. The strings change for every condition. The only common expression is parentheses (). – Habip Oğuz Jun 20 '19 at 15:44
  • So what you want is to find every instance of a pair of parentheses and wrap the contents of the parentheses - regardless of what they are - in bold tags. Do the square brackets figure in at all? – Scott Hannen Jun 20 '19 at 15:50

1 Answers1

2

Use

Regex.Replace(s, @"\(([^()]*)\)", "(<b>$1</b>)")

.NET regex demo

C# demo:

using System;
using System.Text.RegularExpressions;

public class Test
{
    public static void Main()
    {
        var s = "[Whatever You Want (WYW)]";
        Console.WriteLine(Regex.Replace(s, @"\(([^()]*)\)", "(<b>$1</b>)"));
    }
}

Output: [Whatever You Want (<b>WYW</b>)]

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397